1
Answer

DataGrid ClientSideEvents

Photo of william 0

william 0

20y
1.8k
1
Sushila D. Patel - I converted the VB code snippet below to C# as seen in your article: http://www.c-sharpcorner.com/Code/2003/June/DataGridClientSideEvents.asp. It seemed to have worked okay except for the bad line below... This is the bad line: dv = dgContactHistory.DataSource; 'Cannot implicitly convert type 'object' to 'System.Data.DataView' dgContactHistory is a datagrid on my web form. The VB code seems to make this assignment without issue; I thought it would work in C#. I have tried to type-cast this line and it compiles, but the application doesn't like the (DataView) type-cast. Could anyone offer a suggestion? Thank you very much. William C# private void dgContactHistory_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { string strID; DataView dv; DataColumnCollection dc; dv = dgContactHistory.DataSource; dc = dv.Table.Columns; foreach (DataColumn dcCol in dv.Table.Columns) { if ((e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item)) { strID = dgContactHistory.DataKeys[e.Item.ItemIndex].ToString(); e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#99ccff'"); ... VB Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound Dim dv As DataView = DataGrid1.DataSource Dim dcCol As DataColumn Dim dc As DataColumnCollection = dv.Table.Columns Dim strID As String For Each dcCol In dv.Table.Columns If e.Item.ItemType = ListItemType.AlternatingItem Or _ e.Item.ItemType = ListItemType.Item Then strID = DataGrid1.DataKeys(e.Item.ItemIndex) e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#99ccff'")

Answers (1)

0
Photo of william 0
NA 14 0 20y
Sushila D. Patel - the answer to my question was one of mixing data types. I got lost in the maze of DataColumnCollections & DataGridColumnCollection. I was also mixing in DataTable columns and getting my underwear in a bunch. Here is the answer to the problem: private void dgContactHistory_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { string strID; DataGridColumnCollection dc; dc = dgContactHistory.Columns; foreach(DataGridColumn dcCol in dc) { if ((e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item)) { strID = e.Item.Cells[0].Text; e.Item.Cells[dc.IndexOf(dcCol)].Attributes.Add("onmouseover", "this.style.backgroundColor='White';this.style.cursor='hand';" + "window.status='Comment=" + strID + "'"); e.Item.Cells[dc.IndexOf(dcCol)].Attributes.Add("onmouseout", "this.style.backgroundColor='';this.style.cursor='default';" + "window.status=''"); e.Item.Cells[dc.IndexOf(dcCol)].Attributes.Add("onclick", "javascript:window.open('MessageWindow.aspx?id=" + strID + "'," + "'CommentDetails','height=300,width=330')"); };//end of if... };//end of foreach }//end of function