C
Chicagoboy27
I have dynamically bound a field to a gridview using the follwoing
code:
BoundField nameColumn = new BoundField();
nameColumn.DataField = "territory";
nameColumn.HeaderText = "Terr Num";
nameColumn.SortExpression = "territory";
//appends the column at the end
GridView1.Columns.Add(nameColumn);
//Use insert command to notify what column number you would like it at
//GridView1.Columns.Insert(2, nameColumn);
in the gridview I have a template field in the grid..
<asp:TemplateField HeaderText="Name" SortExpression="last_name">
<ItemTemplate>
<asp:Label runat="server" ID="lblLastName"
Text='<%# Eval("last_name")%>'></asp:Label>,
<asp:Label ID="lblFirstName" runat="server"
Text='<%# Eval("first_name")%>'></asp:Label>
<asp:Label ID="lblMidName" runat="server" Text='<%#
Eval("mid_name")%>'></asp:Label>
</ItemTemplate>
I am running into a problem when I try and grab the values of the
template fields using the following code on the selectedindexchanged.
The code below only works when I use GridView1.Columns.Add(nameColumn);
when I try and use
GridView1.Columns.Insert(2, nameColumn);
the code below does not want to work. I am wondering how to dynamically
create the bound column and have it work as the column.add function:
string name =
Convert.ToString(((Label)GridView1.SelectedRow.FindControl("lblLastName")).Text);
name = name + ", " +
Convert.ToString(((Label)GridView1.SelectedRow.FindControl("lblFirstName")).Text);
The error I receive when using columns.insert is
Object reference not set to an instance of an object.
any bump in the right direction would be appreciated
code:
BoundField nameColumn = new BoundField();
nameColumn.DataField = "territory";
nameColumn.HeaderText = "Terr Num";
nameColumn.SortExpression = "territory";
//appends the column at the end
GridView1.Columns.Add(nameColumn);
//Use insert command to notify what column number you would like it at
//GridView1.Columns.Insert(2, nameColumn);
in the gridview I have a template field in the grid..
<asp:TemplateField HeaderText="Name" SortExpression="last_name">
<ItemTemplate>
<asp:Label runat="server" ID="lblLastName"
Text='<%# Eval("last_name")%>'></asp:Label>,
<asp:Label ID="lblFirstName" runat="server"
Text='<%# Eval("first_name")%>'></asp:Label>
<asp:Label ID="lblMidName" runat="server" Text='<%#
Eval("mid_name")%>'></asp:Label>
</ItemTemplate>
I am running into a problem when I try and grab the values of the
template fields using the following code on the selectedindexchanged.
The code below only works when I use GridView1.Columns.Add(nameColumn);
when I try and use
GridView1.Columns.Insert(2, nameColumn);
the code below does not want to work. I am wondering how to dynamically
create the bound column and have it work as the column.add function:
string name =
Convert.ToString(((Label)GridView1.SelectedRow.FindControl("lblLastName")).Text);
name = name + ", " +
Convert.ToString(((Label)GridView1.SelectedRow.FindControl("lblFirstName")).Text);
The error I receive when using columns.insert is
Object reference not set to an instance of an object.
any bump in the right direction would be appreciated