Adding control to datagrid

T

Trond

I have an ASP.NET page that is displaying data from a dataset in a datagrid.
All columns are bound. In code behind file i use ItemDataBound event to do
some formatting of some off the columns. Insted of changing the
cell.BackColor = Color.Green; i want to add a checkbox and set it to checked
if true and unchecked if not ( in the else loop). Is this possible?

Best regards
Trond

private void dgMessages_ItemDataBound_1(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)

{

try

{

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==

ListItemType.AlternatingItem)

{

TableCell cell = e.Item.Cells[7];

long Timediff = long.Parse(cell.Text);

if (Timediff == 1 || Timediff == 3 || Timediff == 5 || Timediff == 7 ||
Timediff == 9 || Timediff==11 || Timediff == 13 ||Timediff == 15 )

{

cell.BackColor = Color.Green;

cell.Text="OK";

}

else

{

cell.BackColor = Color.Red;

cell.Text="-";

}
 
U

ural

Have a
<asp:TemplateColumn HeaderText="Check">
<ItemTemplate>
<asp:CheckBox ID="temp" Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
in your aspx.
in item databound event handler say
(assume this check box is at the 1 column)
System.Web.UI.WebControls.CheckBox objChck =
(System.Web.UI.WebControls.CheckBox)e.Item.Cells[1].FindControl("temp");

Then based on your condition check or uncheck this objChck.

Hope this helps!!
Ural
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top