I have a gridview that contains some built in columns and some dynamic columns. when I add dynamic columns to gridview,template field column got empty!!why?
private void BindGrid(long ExId)
{ //remove previously dynamically added columns while (gridSchools.Columns.Count != 6)
{
gridSchools.Columns.RemoveAt(5);
}
List<Course> crses = CourseManager.GetCourseByExamGroupInLevelId(ExId); //add new dynamic columns after fourth column for (int i = 0; i < crses.Count; i++)
{
gridSchools.Columns.Insert(i + 5, new BoundField
{
DataField = crses[i].Title,
HeaderText = crses[i].Title
});
}
}
gridSchools.DataBind();
upnlGrid.Update();
}
After rebind grid(there is no problem at first time) all data are shown normally exept last column that is item template. I changed third column from bound filed to template field for test. I saw same issue for it. I set a breakpoint and saw item template
of template field in my column have been set to null??
while (gridSchools.Columns.Count != 6)
{
gridSchools.Columns.RemoveAt(5);
}
Sorry I don't make myself so clear——I mean:
If your columns.Count is less than 5 (if 4). How can you remove At 5?
The grid columns count is always at least 6. because I have 6 static clolumns ("Numb" ,"Name", "RegDate", "Status", "StudentsCount", "Get Again") and
some dynamic columns between "StudentsCount" and "Get Again". so for removing dynamic columns I've just remove fifth column until columns count get 6.
petercory
Member
1 Points
13 Posts
template field got empty after adding some columns
Feb 04, 2013 08:09 AM|LINK
I have a gridview that contains some built in columns and some dynamic columns. when I add dynamic columns to gridview,template field column got empty!!why?
You can download my simple website from http://uploadtak.com/images/i7193_TestGrid.rar or from SkyDrive http://sdrv.ms/14Uh1y3
aspx code:
<asp:GridView ID="gridSchools" runat="server" DataSourceID="objdtSchools"> <Columns> <asp:BoundField HeaderText="Numb" ReadOnly="true" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="RegDate" HeaderText="Date" /> <asp:BoundField HeaderText="Status" SortExpression="Status" DataField="StatusText" /> <asp:BoundField DataField="StudentsCount" SortExpression="StudentsCount" HeaderText="Counts" /> <asp:TemplateField HeaderText="Get Again"> <ItemTemplate> <asp:ImageButton runat="server" ImageUrl="/GlobalImages/DisplayUp.png" CommandName="GetData" CommandArgument='<%# Eval("Url") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>cs code:
private void BindGrid(long ExId) {//remove previously dynamically added columns
while (gridSchools.Columns.Count != 6) { gridSchools.Columns.RemoveAt(5); } List<Course> crses = CourseManager.GetCourseByExamGroupInLevelId(ExId);
//add new dynamic columns after fourth column
for (int i = 0; i < crses.Count; i++) { gridSchools.Columns.Insert(i + 5, new BoundField { DataField = crses[i].Title, HeaderText = crses[i].Title }); } } gridSchools.DataBind(); upnlGrid.Update(); }
After rebind grid(there is no problem at first time) all data are shown normally exept last column that is item template. I changed third column from bound filed to template field for test. I saw same issue for it. I set a breakpoint and saw item template of template field in my column have been set to null??
vinz
All-Star
126896 Points
17922 Posts
MVP
Re: template field got empty after adding some columns
Feb 04, 2013 12:34 PM|LINK
When you say "Empty" do you mean the column is hidden or there's just no data showing?
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: template field got empty after adding some columns
Feb 06, 2013 12:02 AM|LINK
Hi,
This snippet of codes look very strange, you've used a for to loop each column by index, but you never use "i", why?
Can you tell us your thoughts about this loop?
petercory
Member
1 Points
13 Posts
Re: template field got empty after adding some columns
Feb 06, 2013 02:15 AM|LINK
The column is visible but there isn't any data in it.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: template field got empty after adding some columns
Feb 06, 2013 03:29 AM|LINK
Where do you put the Bind method?
petercory
Member
1 Points
13 Posts
Re: template field got empty after adding some columns
Feb 06, 2013 04:54 AM|LINK
Yes, my code was strange so I edit it like this:
while (gridSchools.Columns.Count != 6) { gridSchools.Columns.RemoveAt(5); }at these lines I want to remove previously added dynamic columns from grid.
petercory
Member
1 Points
13 Posts
Re: template field got empty after adding some columns
Feb 06, 2013 04:54 AM|LINK
I am using object data source:
<asp:ObjectDataSource ID="objdtSchools" runat="server" EnablePaging="True" SelectMethod="GetDataCollection" SelectCountMethod="GetDataCollectionCount" TypeName="ReportManager"> </asp:ObjectDataSource>and I've written "GetDataCollection" like this:
public static DataTable GetDataCollection(int startRowIndex, int maximumRows) { //write some codes to make a data table }Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: template field got empty after adding some columns
Feb 06, 2013 05:42 AM|LINK
Sorry I don't make myself so clear——I mean:
If your columns.Count is less than 5 (if 4). How can you remove At 5?
petercory
Member
1 Points
13 Posts
Re: template field got empty after adding some columns
Feb 06, 2013 08:35 AM|LINK
The grid columns count is always at least 6. because I have 6 static clolumns ("Numb" ,"Name", "RegDate", "Status", "StudentsCount", "Get Again") and some dynamic columns between "StudentsCount" and "Get Again". so for removing dynamic columns I've just remove fifth column until columns count get 6.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: template field got empty after adding some columns
Feb 06, 2013 11:30 AM|LINK
Hi again,
Where do you put the Bind method?