Groups | Blog | Home
all groups > asp.net datagrid control > november 2004 >

asp.net datagrid control : Datagrid & checkbox column


et
11/30/2004 2:08:13 PM
All the articles on checkboxes in a datagrid revolve around using the
checkboxes for user selection.

I need to have a checkbox inserted and its value based on a the dataset the
grid is bound to. How do I retrieve that value?

This is my datagrid:
<asp:datagrid id=dg runat="server" AutoGenerateColumns="False"
CellPadding="3" BackColor="White" BorderColor="#999999" BorderWidth="1px"
GridLines="Vertical" BorderStyle="None" >
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn Visible="False" DataField="ID"
ReadOnly="True"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="FeePaid">
<ItemTemplate>
<%#Container.DataItem("FeePaid")%>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox runat="server" ID="chkFeePaid"/>
</EditItemTemplate>
</asp:TemplateColumn>
</asp:datagrid

I don't know what to put in the code to get the checkbox, upon editing the
row, to get the value of the cell, then check or uncheck the checkbox
accordingly. I would also need to update the dataset and database with the
new value.

user NO[at]SPAM domain.invalid
12/5/2004 12:12:03 PM
[quoted text, click to view]

Hi et,
I had some code to do just that - I'm sure you can figure it out, but if you
need clarification let me know.

private void dgdRegions_ItemCreated(
object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drvTemp = (DataRowView)e.Item.DataItem;
CheckBox chkCreate = (CheckBox)e.Item.FindControl("chkTemp");
chkCreate.Checked = bool.Parse(drvTemp["contract"].ToString());
}
}

I have a little datagrid demo with checkboxes, paging and dropdowns on my
website. You can download it from http://mamakin1976.plus.com/download.

HTH

--
Software is like sex: it's better when it's free -- [Linus Torvalds]

Fabio Marini - A+, RHCT, MCDBA, MCAD.NET
AddThis Social Bookmark Button