Adding control to datagrid

Discussion in 'ASP .Net Web Controls' started by Trond, May 4, 2005.

  1. Trond

    Trond Guest

    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="-";

    }
     
    Trond, May 4, 2005
    #1
    1. Advertising

  2. Trond

    ural Guest

    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
    "Trond" wrote:

    > 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="-";
    >
    > }
    >
    >
    >
     
    ural, May 4, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AC
    Replies:
    0
    Views:
    338
  2. =?Utf-8?B?QXNoYQ==?=

    adding a control to a datagrid dynamically...

    =?Utf-8?B?QXNoYQ==?=, Jul 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    345
    Mr. Dot Net
    Jul 19, 2004
  3. Sjaakie Helderhorst

    Adding javascript event to datagrid control

    Sjaakie Helderhorst, Jul 26, 2004, in forum: ASP .Net
    Replies:
    19
    Views:
    5,403
    Shiva
    Jul 26, 2004
  4. Jc Morin
    Replies:
    1
    Views:
    200
    Jc Morin
    Dec 5, 2003
  5. Replies:
    1
    Views:
    352
Loading...

Share This Page