1
Reply

ItemDataBound issue with foreach

william 0

william 0

May 23 2004 4:42 PM
2k
Anyone... I converted the VB code snippet below to C#. 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)