Accessing value of bound column at run time

J

Justin Dyer

I have a DataGrid that has a checkbox and a bound column. When the user
clicks a button, I want to iterate through all the items in the datagrid and
display the text:
[value of databound column] is/is not checked. How do I do that? Thanks in
advance for your help.

Here is my aspx code:


<asp:DataGrid id="DataGrid1"
style="Z-INDEX: 101; LEFT: 176px; POSITION: absolute;
TOP: 112px"
runat="server"
AutoGenerateColumns="False"
EnableViewState="True">
<ItemStyle BackColor="Silver"></ItemStyle>
<HeaderStyle ForeColor="Transparent"
BackColor="SandyBrown"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Include?">
<ItemTemplate>
<asp:CheckBox id="IncludeItem"
EnableViewState="True"
Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="ItemDesc"
HeaderText="Item Description">
</asp:BoundColumn>
</Columns>
</asp:DataGrid>

Here is my C# code:

foreach (DataGridItem dgiItem in DataGrid1.Items)
{
// Make sure this is an item and not a header or footer
if ((dgiItem.ItemType == ListItemType.Item) ||
(dgiItem.ItemType == ListItemType.AlternatingItem))
{
tc = dgiItem.Cells;

if (((CheckBox)(dgiItem.FindControl("IncludeItem"))).Checked)
{
lblResult.Text += "is checked.\n"; // I want "value of ItemDesc is
checked"
}
else
{
lblResult.Text += "is not checked.\n"; // I want "value of ItemDesc
is not checked"
}


}


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top