I have the following asp:GridView
<asp:GridView ID="contacts" runat="server" AutoGenerateColumns="false" Width="100%">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:HiddenField ID="ContactID" runat="server" Value='<%# Bind("employerdetailcontactid") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Action" ItemStyle-CssClass="center" >
<ItemTemplate>
<asp:Label ID="ActionLabel" runat="server" Text='<%# Bind("action") %>' />
<asp:CheckBox ID="Action" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="First Name">
<ItemTemplate>
<asp:TextBox ID="FirstName" runat="server" Text='<%# Bind("firstname") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Last Name">
<ItemTemplate>
<asp:TextBox ID="LastName" runat="server" Text='<%# Bind("lastname") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="E-Mail">
<ItemTemplate>
<asp:TextBox ID="Email" runat="server" Text='<%# Bind("email") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Phone Number">
<ItemTemplate>
<asp:TextBox ID="PhoneNumber" runat="server" Text='<%# Bind("phonenumber") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Send E-mail" ItemStyle-CssClass="center" >
<ItemTemplate>
<asp:CheckBox ID="SendEmail" runat="server" Checked='<%# Convert.ToBoolean( Eval("sendemail")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The GridView has however many rows from the database, plus one extra row for adding a new row. Very rarely will the end-user ever add more than one contact at a time, so I am not going to bother having an "Add Row" button. I want the rows from the database to have the label say "Delete" and the manually added row have the label say "New". Here is my code to populate the GridView:
SqlCommand cmd = new SqlCommand("SELECT *,'Delete' as action FROM contact WHERE employerid=@employerid", conn);
cmd.Parameters.AddWithValue("@employerid", id);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
//These are added to be able to manually add a row below
dt.Columns.Add("ContactID");
dt.Columns.Add("Action");
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
dt.Columns.Add("Email");
dt.Columns.Add("PhoneNumber");
dt.Columns.Add("SendEmail");
dt.Load(dr);
DataRow drow = dt.NewRow();
drow["ContactID"] = 0;
drow["Action"] = false;
drow["FirstName"] = "";
drow["LastName"] = "";
drow["Email"] = "";
drow["PhoneNumber"] = "";
drow["SendEmail"] = "False";
dt.Rows.Add(drow);
contacts.DataSource = dt;
contacts.DataBind();
How would I set the text of ActionLabel to "New" for the manually added row ?
CodePudding user response:
You have two options:
- Attach for the RowDataBound event and programmatically set the text
<asp:TemplateField HeaderText ="Action" ItemStyle-CssClass="center" >
<ItemTemplate>
<asp:Label ID="ActionLabel" runat="server" Text='<%# Bind("action") %>' />
<asp:CheckBox ID="Action" runat="server" />
</ItemTemplate>
</asp:TemplateField>
//use the onItemDatabound
void contacts_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// get contact id to identify whether this is fake row
int contactId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ContactID"));
string labelText = contactId > 0 ? "Delete" : "New";
((Label)e.Item.FindControl("ActionLabel")).Text = labelText;
}
}
- Option 2: Use FooterRow template and enable footer row in the GridView component
<asp:GridView ShowFooter = "true"
...
<asp:TemplateField HeaderText ="Action" ItemStyle-CssClass="center" >
<ItemTemplate>
<asp:Label ID="ActionLabel" runat="server" Text="Delete" />
<asp:CheckBox ID="Action" runat="server" />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="ActionLabel" runat="server" Text="New" />
<asp:CheckBox ID="Action" runat="server" />
</FooterTemplate>
</asp:TemplateField>
I am a little confused whether I modified the right GridView column, but I think you'll get the idea