Click to See Complete Forum and Search --> : Help in DataGrid please, URGENT
fongjeffrey
July 23rd, 2003, 12:23 PM
Hi all,
I have encounted two problem in using DataGrid. I am using C#. 1. Sorting and 2. Delete selected row
1. Sorting - I have defined a dataset1 and dataview1, and dataview.table = dataset1.table1. In SortCommand(), I have the following: (sortType is a static int )
private void datagrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
if ( ( sortType % 2) == 0 )
this.dvCompany.Sort = e.SortExpression;
else
this.dvCompany.Sort = e.SortExpression + " DESC";
sortType++;
DataBind();
}
The following is my select_coommand after I clicked on the pre-defined Select link button in datagrid1:
private void datagrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
string myCompany, myDispOrder, myImageURL, editInfo;
myCompany = datagrid1.SelectedItem.Cells[1].Text;
myDispOrder = datagrid1.SelectedItem.Cells[2].Text;
editInfo = myCompany + "%" + myDispOrder + "%";
Response.Redirect("page2.aspx?editInfo=" + editInfo,
true);
}
Before I clicked on the column header for sorting, I clicked on the select button and both "myCompany" and "myDispOrder" are obtained the right values. However, after sorting the header, the data displayed in datagrid1 is sorted correctly, but when I clicked on the select button, both "myCompany" and "myDispOrder" are still set to the value before it is sorted, i.e. they still have the values before sorting and so same data is obtained before or after sorting in datagrid1. Can anyone tell me what is not right?
2. datagrid_DeleteCommand(). Can anyone tell me how to implement on DeleteCommand() to delete the select row in datagrid1 and the actual database?
Sorry for the long question, coz I am just a beginner in ASP .net and ADO .net
Thank you so much and any help will be greatly appreciated
Rohit Kukreti
July 24th, 2003, 05:34 AM
Hi fongjeffrey,
1. DataGrid Sort prob: -
I feel u need to move ur pageindex to the first page using the code below
DataGrid1.EditItemIndex = -1;
DataGrid1.CurrentPageIndex= 0;
2. Delete Prob: -
Try and insert a DataKeyField in ur DataGrid tag in ur html src.
and use this value in the where clause of ur sql query.
I feel using both the above soln. shud solve ur prob.
3. Implementing DataGrid_Delete command
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid_Delete);
insert the above line in ur InitializeComponent fn.
Hope this helps. If not then feel free to post further qts.
---
Rohit
fongjeffrey
July 24th, 2003, 06:13 PM
Thanks for your reply.
In respond to the sort problem, u said I need to move the pageindex to the first page; however when I do sorting in datagrid, there was only one page, and after I did sorting, I stayed in the same page, and the data was sorted correctly in the datagrid display. The only problem was when I tried to retrieve the sorted data from the datagrid, I still got the data value before sorting. Do u think this is related to page index? coz all data remains in the same page.
2. I have question when deleting a row in datagrid. Since the data source of my datagrid is set to a dataview which is generated from a dataset. when I delete a row in datagrid, do I need to delete it in a dataview as well datset? How do I delete the entire row in datagrid, dataview and dataset?
Sorry about that since I am just a beginner in asp .net
Again, thank you so much for you help and I am appreciated!
fongjeffrey
July 25th, 2003, 03:49 AM
Hi Rohit Kukreti,
I've tried what u suggested me to do, but it did not work. I don't know if I do correctly. the following is page_load():
private void Page_Load(object sender, System.EventArgs e)
{
if ( this.IsPostBack == true )
{
this.dsCompany1 = (dsCompany) ViewState["dsComp"];
}
else
{
this.daCompany.Fill(this.dsCompany.Company);
ViewState["dsComp"] = this.dsCompany;
}
this.DataBind();
}
the problem is that after I sorted a column in datagrid, I will call
"this.dsCompany1 = (dsCompany) ViewState["dsComp"];" and then my datagrid lost all data and empty. But if I remove the conditional check "
if ( this.IsPostBack == true )
{
this.dsCompany1 = (dsCompany) ViewState["dsComp"];
}
i would call "this.daCompany.Fill(this.dsCompany.Company);" everytime I sorted the data. So I always have the original data before sorting. Can anyone tell me how to solve this problem?
Thank you so much and I am really appreciated
Jeffrey
Rohit Kukreti
July 25th, 2003, 04:01 AM
Hi Jeffrey,
I'll certainly help u. But pls gimme sometime have got few of my own things to complete. But, i'll tell u how i go abt doing this
1. On Page load i check whether it is a post back or not.
If yes then i apply the filters i.e. rowfilter, sortfilter to the dataview and bind the grid. But, u need to bind the grid bfor this step normally. U go BindGrid() and then do dv.RowFilter=ViewState["LastFilter"].ToString(). and then he sort thing follows
If u r not very clear on this then post back i'll give u the whole code but gimme sometime to give u that
hope this helps
---
Rohit
fongjeffrey
July 25th, 2003, 01:22 PM
hi Rohit Kukreti,
Thank you very much for very help. I am sorry if it is impossible that you can explain it in more detail. However, please get ur stuff done first.
Thank you so much again and I am really appreciated!
Jeffrey
Rohit Kukreti
July 26th, 2003, 06:54 AM
Hi Jeffrey,
Use the logic given below in your code. I use the same logic and it is working for me nicely
Page_Load
{
string lastFilter="",lastSortColumn="",lastSortOrder="";
if (Page.IsPostBack)
{
lastFilter= (string)ViewState["LastFilter"];
lastSortColumn= (string)ViewState["LastSortColumn"];
lastSortOrder= (string)ViewState["LastSortOrder"];
}
else
{
ViewState["LastSortOrder"]="";
ViewState["LastSortColumn"]= "";
ViewState["LastFilter"] = "";
}
if (ViewState["LastSortColumn"].ToString() !="")
dv.Sort= lastSortColumn+ " "+ lastSortOrder;
bind(); //call this fn to bind data to ur grid. Wud give the fn later
if (lastFilter != "")
{
dv.RowFilter= lastFilter;
DataGrid1.DataSource =dv; //dv=dataview
DataGrid1.DataBind();
}
}
bind()
{
SqlDataAdapter myCommand = new SqlDataAdapter(SelectCommand, myConnection); //myConnection=Con obj and SelectCommand ur select query
ds = new DataSet();
myCommand.Fill(ds);
dv = ds.Tables[0].DefaultView ;
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
}
btnFilter_Click()
{
ViewState["LastFilter"] = strFilter; //strFilter ur filter cond.
dv.RowFilter= strFilter;
DataGrid1.EditItemIndex = -1;
DataGrid1.CurrentPageIndex= 0; // important! goto first page
DataGrid1.DataBind();
}
SortCommand()
{
ViewState["LastSortOrder"]= newSortOrder; //ur sort order
ViewState["LastSortColumn"]= newSortColumn; //ur sort column
bind();
if (ViewState["LastFilter"].ToString() != "")
dv.RowFilter = ViewState["LastFilter"].ToString();
dv.Sort= newSortColumn+ " "+ newSortOrder;
DataGrid1.EditItemIndex = -1;
DataGrid1.CurrentPageIndex= 0; // goto first page
DataGrid1.DataBind();
}
hope this helps
---
Rohit
fongjeffrey
July 26th, 2003, 01:59 PM
thank you so so so much, Rohit Kukreti!!!!
The code works great. I have got the sorting part working properly. And now I am working on the delete_command().
the right steps for delete is:
1. delete the selected row in datagrid
2. delete the row in physical db
3. update dataset
4. update the dataview
am I having the right appraoch for deletion?
Sorry for more questions and questions!
I am really appreciated for ur help and have a great weekend
Jeffrey
Rohit Kukreti
July 28th, 2003, 03:46 AM
Hi Jeffrey,
Felt gr8 after hearing that my code helped u. Anyways, the steps u metioned for deletion of recs it seems they'll give u the desired results. But, I follow a diff. approach. Don't know its right to do that or is the fastest but yes has least minimal steps and works gr8 for me.
If ur using the fns. i gave u then this is how i do it
Delete_Command(object sender, DataGridCommandEventArgs e)
{
int keyValue = (int)DataGrid1.DataKeys[e.Item.ItemIndex];//can be casted to tring if ur DataKey field is string
SqlCommand DeleteCommand = new SqlCommand("DELETE from Table1 where Table1id=" + keyValue , myConnection);
DeleteCommand.ExecuteNonQuery();
DataGrid1.CurrentPageIndex = 0;
DataGrid1.EditItemIndex = -1;
BindGrid();
}
Just try this and look if this is faster and less complicated. If u feel ur approach is better than this pls let me know the advtgs. so that from next time i can also use the same 1.
Sorry for more questions and questions! -- no probs. Actually i look forward to good qts. And also, u can call me Rohit
hope this helps
---
Rohit
fongjeffrey
August 1st, 2003, 12:15 AM
Hi Rohit ,
I think your approach is simpler. Thank you for all ur help. You relieve me a lot of pain.
I have noticed a rare problem. Everytime when I clicked on the select button or delete button or any hyperlink in my datagrid, the text will turn to red (indicated the linked is clicked before); however it never turns back to blue even I close the browser and start the debug mode again. Do u know how to solve this problem?
Thank you again.!!!
Jeff.
Rohit Kukreti
August 1st, 2003, 03:43 AM
Hi Jeffrey,
U need to add a style tag to ur aspx page. It comes under <head></head> tag and next to <title> tag
<style>
A:link { FONT-SIZE: 8pt; COLOR: blue; LINE-HEIGHT: 9pt; FONT-FAMILY: verdana; TEXT-DECORATION: none }
A:visited { FONT-SIZE: 8pt; COLOR: blue; LINE-HEIGHT: 9pt; FONT-FAMILY: verdana; TEXT-DECORATION: none }
A:active { FONT-SIZE: 8pt; COLOR: blue; LINE-HEIGHT: 10pt; FONT-FAMILY: verdana; TEXT-DECORATION: none }
</style>
This wud keep ur links blue even if they r clicked
hope this helps
---
Rohit
Rohit Kukreti
August 1st, 2003, 03:46 AM
Hi,
I don't know whats happening cudn't see the whole post so again posting it.
Just do View->Source u'll get my ans
fongjeffrey
August 3rd, 2003, 02:41 PM
Sorry. I don't get u. Can u put it a litt more detail how I can add a style tag to my datagrid?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.