I have never seen anything like this. I am binding a datatable to a datagrid that has a dropdown in one of the ItemTemplate columns. The datatable has 10 rows in it (I checked). However, it only hits the ItemDataBound event once and only shows one row
- coincidently its the last row in the datatable.
Does anyone know what might be going on here? I have not specified a DataKey for the grid and could that be the problem?
Thanks,
Greg
"Providing software solutions for your world!"
Please mark my response as the answer if it helped you.
If you are absolutely sure that dtWork contains 10 rows when databinding starts, is it that no exception is raised? It works otherwise normally but displays only 1 row? Did you try calling dtWork.AcceptChanges(); just before databinding, e.g making sure it
is called also from the other code path?
Right before .DataBind(), I checked dtWork.Rows.Count() and it is 10. No exception occurs. Here is my aspx code for the grid. Maybe the problem is in there.
OK. I tried binding to dsObjects.Table[0] and that works just fine. So it is somewhere where I am trying to bind a datatable directly to a datagrid. Do I need to add it to a dataset or give the datatable a name or something? I haven't ever crossed this
issue before so it seems weird. Going back in...
"Providing software solutions for your world!"
Please mark my response as the answer if it helped you.
OK. I figured out what it was and I'm still confused.
The problem was that the RowFilter was not being cleared before databinding. However, that brings me to a question: In the code above, I am assigning the DataTable.DefaultView to a dv Dataview. I would have thought that this would be a new object and
not directly referenced by the datatable. So when I do dv=dtWork.DefaultView(), any changes that are made to dv will be reflected in dtWork which means that it creates the dataview byref instead of byval.
Can someone confirm this is the case? I guess it makes sense since I am doing an = but since I usually do some coding in vb.net, maybe I am thinking that vb handles it differently?
Thanks for any helpful insight.
Greg
"Providing software solutions for your world!"
Please mark my response as the answer if it helped you.
It's because databinding occurs actually to the DataView got via DefaultView property. Databinding doesn't in fact ever happen directly to DataTable. DefaultView on the other hand is one and the same object (DataTable creates it with lazy init approach and
then returns the same obj when member contains a view)
gknierim
Contributor
2008 Points
492 Posts
DataBinding DataTable only shows first row
Feb 16, 2007 06:54 PM|LINK
I have never seen anything like this. I am binding a datatable to a datagrid that has a dropdown in one of the ItemTemplate columns. The datatable has 10 rows in it (I checked). However, it only hits the ItemDataBound event once and only shows one row - coincidently its the last row in the datatable.
Does anyone know what might be going on here? I have not specified a DataKey for the grid and could that be the problem?
Thanks,
Greg
Please mark my response as the answer if it helped you.
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: DataBinding DataTable only shows first row
Feb 16, 2007 06:56 PM|LINK
Hi,
Post the code
Teemu Keiski
Finland, EU
gknierim
Contributor
2008 Points
492 Posts
Re: DataBinding DataTable only shows first row
Feb 16, 2007 06:58 PM|LINK
private void LoadAssignments() { DataTable dtCognosUsers; DataSet dsObjects = new DataSet(); DataTable dtWork = new DataTable(); DataRow dRow; DataView dv; _Assignment objA = new _Assignment(); dtWork.Columns.Add("ObjectId", System.Type.GetType("System.String")); dtWork.Columns.Add("ObjectCAMID", System.Type.GetType("System.String")); dtWork.Columns.Add("ObjectName", System.Type.GetType("System.String")); dtWork.Columns.Add("RoleName", System.Type.GetType("System.String")); try { dtCognosUsers = _SDK.GetUsersFromCognos(false); for (int ix=0;ix// Add rows to dtWork datatable dRow = dtWork.NewRow(); dRow["ObjectId"] = ""; dRow["ObjectCAMID"] = dtCognosUsers.Rows[ix]["CAMID"].ToString(); dRow["ObjectName"] = dtCognosUsers.Rows[ix]["UserName"].ToString(); dRow["RoleName"] = "None"; dtWork.Rows.Add(dRow); } objA.ScorecardId = Convert.ToInt32(ddlScorecards.SelectedValue); objA.AssignType = rblAssignBy.SelectedValue; objA.ObjectId = Convert.ToInt32(ddlObjects.SelectedValue); objA.FilterText = txtFilter.Text; if (objA.SelectAssignments(ref dsObjects)) { // Take first datatable and add to dtWork dv = dtWork.DefaultView; for (int ix=0;ix""; dv.RowFilter = "ObjectCAMID='" + dsObjects.Tables[0].Rows[ix]["ObjectCAMID"].ToString() + "'"; if (dv.Count == 0) { // Group or User doesn't exist in PERFORM Users group so add to datatable dRow = dtWork.NewRow(); dRow["ObjectId"] = dsObjects.Tables[0].Rows[ix]["ObjectId"].ToString(); dRow["ObjectCAMID"] = dsObjects.Tables[0].Rows[ix]["ObjectCAMID"].ToString(); dRow["ObjectName"] = dsObjects.Tables[0].Rows[ix]["ObjectName"].ToString(); dRow["RoleName"] = "None"; dtWork.Rows.Add(dRow); } else { // User (should be just user) exists so update ObjectId for (int iy=0;iyif (dtWork.Rows[iy]["ObjectCAMID"].ToString() == dsObjects.Tables[0].Rows[ix]["ObjectCAMID"].ToString()) { dtWork.Rows[iy]["ObjectId"] = dsObjects.Tables[0].Rows[ix]["ObjectId"].ToString(); break; } } dtWork.AcceptChanges(); } } // Bind to datagrid dgAssignments.DataSource = dtWork; dgAssignments.DataBind(); } } catch (Exception ex) { lblError.Text = _Global.FormatException(ex, "Error loading assignments"); } finally { dtCognosUsers = null; dsObjects = null; dtWork = null; } } private void dgAssignments_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { // Bind datatable of Roles to fill each dropdownlist in datagrid DropDownList objDDL = ((DropDownList)e.Item.Cells[2].FindControl("ddlRole")); objDDL.DataSource = (DataTable)dtRoles; objDDL.DataBind(); } }Please mark my response as the answer if it helped you.
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: DataBinding DataTable only shows first row
Feb 17, 2007 06:25 AM|LINK
Teemu Keiski
Finland, EU
gknierim
Contributor
2008 Points
492 Posts
Re: DataBinding DataTable only shows first row
Feb 20, 2007 01:12 PM|LINK
Right before .DataBind(), I checked dtWork.Rows.Count() and it is 10. No exception occurs. Here is my aspx code for the grid. Maybe the problem is in there.
Please mark my response as the answer if it helped you.
gknierim
Contributor
2008 Points
492 Posts
Re: DataBinding DataTable only shows first row
Feb 20, 2007 01:43 PM|LINK
Please mark my response as the answer if it helped you.
gknierim
Contributor
2008 Points
492 Posts
Re: DataBinding DataTable only shows first row
Feb 20, 2007 01:51 PM|LINK
Nope, the issue wasn't the index.
I also tried removing the template column and adding a DataKey and neither helped.
I have not seen anything like this. Anyone have any ideas?
Thanks,
Greg
Please mark my response as the answer if it helped you.
gknierim
Contributor
2008 Points
492 Posts
Re: DataBinding DataTable only shows first row
Feb 20, 2007 01:59 PM|LINK
Please mark my response as the answer if it helped you.
gknierim
Contributor
2008 Points
492 Posts
Re: DataBinding DataTable only shows first row
Feb 20, 2007 02:48 PM|LINK
OK. I figured out what it was and I'm still confused.
The problem was that the RowFilter was not being cleared before databinding. However, that brings me to a question: In the code above, I am assigning the DataTable.DefaultView to a dv Dataview. I would have thought that this would be a new object and not directly referenced by the datatable. So when I do dv=dtWork.DefaultView(), any changes that are made to dv will be reflected in dtWork which means that it creates the dataview byref instead of byval.
Can someone confirm this is the case? I guess it makes sense since I am doing an = but since I usually do some coding in vb.net, maybe I am thinking that vb handles it differently?
Thanks for any helpful insight.
Greg
Please mark my response as the answer if it helped you.
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: DataBinding DataTable only shows first row
Feb 20, 2007 03:31 PM|LINK
It's because databinding occurs actually to the DataView got via DefaultView property. Databinding doesn't in fact ever happen directly to DataTable. DefaultView on the other hand is one and the same object (DataTable creates it with lazy init approach and then returns the same obj when member contains a view)
Teemu Keiski
Finland, EU