Problem sorting a Datagrid with Checkbox columns (checkboxes not following sort)

Discussion in 'ASP .Net' started by Scott, Aug 8, 2007.

  1. Scott

    Scott Guest

    I am trying to sort a datagrid which contains 5 checkbox columns. All
    other columns sort properly except these 5 checkbox columns which dont
    sort at all.

    I am using ASP.NET 1.1 and this code is located in a user control.

    The code snippet for binding the checkbox control is as follows and is
    found in the ItemDataBound event for the datagrid. It appears that
    the index value never changes...it always seems to reference the
    orginal dataset index.

    Any help would be greatly appreciated.

    try
    {
    System.Web.UI.WebControls.Image img1 =
    (System.Web.UI.WebControls.Image)e.Item.Cells[7].Controls[1];
    int iIndx = e.Item.DataSetIndex;
    /*
    0 Disabled
    1 Approved
    2 Waiting For Approval
    3 Rejected
    4 Locked
    */
    int iResultSR = dsListBuilders.Tables["bb_saved_builders"].Rows[iIndx]
    ["sales_rep_approved"].ToString() == string.Empty?
    2:int.Parse(dsListBuilders.Tables["bb_saved_builders"].Rows[iIndx]
    ["sales_rep_approved"].ToString());

    switch(iResultSR)
    {
    case 0:
    img1.ImageUrl="../../images/greybox.gif";
    break;
    case 1:
    img1.ImageUrl="../../images/checkboxyes.gif";
    break;
    case 2:
    img1.ImageUrl="../../images/checkboxno.gif";
    break;
    case 3:
    img1.ImageUrl="../../images/redcheckboxno.gif";
    break;
    case 4:
    img1.ImageUrl="../../images/lock.gif";
    break;
    default:
    img1.ImageUrl="../../images/checkboxno.gif";
    break;
    }
    }
    catch(Exception exBinding1)
    {
    //ignore...
    }
    Scott, Aug 8, 2007
    #1
    1. Advertising

  2. RE: Problem sorting a Datagrid with Checkbox columns (checkboxes not f

    Scott,

    Instead of locating the data in the original table
    dsListBuilders.Tables["bb_saved_builders"].Rows[iIndx]
    use the grid source data:
    DataRow row = (e.Item.ItemData is DataRow) ? e.Item.ItemData as DataRow :
    (e.Item.ItemData as DataRowView).Row;
    int iResultSR = (row["sales_rep_approved"].ToString() ==
    string.Empty)?2:int.Parse(row["sales_rep_approved"].ToString());



    "Scott" wrote:

    > I am trying to sort a datagrid which contains 5 checkbox columns. All
    > other columns sort properly except these 5 checkbox columns which dont
    > sort at all.
    >
    > I am using ASP.NET 1.1 and this code is located in a user control.
    >
    > The code snippet for binding the checkbox control is as follows and is
    > found in the ItemDataBound event for the datagrid. It appears that
    > the index value never changes...it always seems to reference the
    > orginal dataset index.
    >
    > Any help would be greatly appreciated.
    >
    > try
    > {
    > System.Web.UI.WebControls.Image img1 =
    > (System.Web.UI.WebControls.Image)e.Item.Cells[7].Controls[1];
    > int iIndx = e.Item.DataSetIndex;
    > /*
    > 0 Disabled
    > 1 Approved
    > 2 Waiting For Approval
    > 3 Rejected
    > 4 Locked
    > */
    > int iResultSR = dsListBuilders.Tables["bb_saved_builders"].Rows[iIndx]
    > ["sales_rep_approved"].ToString() == string.Empty?
    > 2:int.Parse(dsListBuilders.Tables["bb_saved_builders"].Rows[iIndx]
    > ["sales_rep_approved"].ToString());
    >
    > switch(iResultSR)
    > {
    > case 0:
    > img1.ImageUrl="../../images/greybox.gif";
    > break;
    > case 1:
    > img1.ImageUrl="../../images/checkboxyes.gif";
    > break;
    > case 2:
    > img1.ImageUrl="../../images/checkboxno.gif";
    > break;
    > case 3:
    > img1.ImageUrl="../../images/redcheckboxno.gif";
    > break;
    > case 4:
    > img1.ImageUrl="../../images/lock.gif";
    > break;
    > default:
    > img1.ImageUrl="../../images/checkboxno.gif";
    > break;
    > }
    > }
    > catch(Exception exBinding1)
    > {
    > //ignore...
    > }
    >
    >
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=, Aug 9, 2007
    #2
    1. Advertising

  3. Scott

    Scott Guest

    Re: Problem sorting a Datagrid with Checkbox columns (checkboxes not f

    On Aug 8, 10:26 pm, Sergey Poberezovskiy
    <> wrote:
    > Scott,
    >
    > Instead of locating the data in the original table
    > dsListBuilders.Tables["bb_saved_builders"].Rows[iIndx]
    > use the grid source data:
    > DataRow row = (e.Item.ItemData is DataRow) ? e.Item.ItemData as DataRow :
    > (e.Item.ItemData as DataRowView).Row;
    > int iResultSR = (row["sales_rep_approved"].ToString() ==
    > string.Empty)?2:int.Parse(row["sales_rep_approved"].ToString());
    >
    >
    >
    > "Scott" wrote:
    > > I am trying to sort a datagrid which contains 5 checkbox columns. All
    > > other columns sort properly except these 5 checkbox columns which dont
    > > sort at all.

    >
    > > I am using ASP.NET 1.1 and this code is located in a user control.

    >
    > > The code snippet for binding the checkbox control is as follows and is
    > > found in the ItemDataBound event for the datagrid. It appears that
    > > the index value never changes...it always seems to reference the
    > > orginal dataset index.

    >
    > > Any help would be greatly appreciated.

    >
    > > try
    > > {
    > > System.Web.UI.WebControls.Image img1 =
    > > (System.Web.UI.WebControls.Image)e.Item.Cells[7].Controls[1];
    > > int iIndx = e.Item.DataSetIndex;
    > > /*
    > > 0 Disabled
    > > 1 Approved
    > > 2 Waiting For Approval
    > > 3 Rejected
    > > 4 Locked
    > > */
    > > int iResultSR = dsListBuilders.Tables["bb_saved_builders"].Rows[iIndx]
    > > ["sales_rep_approved"].ToString() == string.Empty?
    > > 2:int.Parse(dsListBuilders.Tables["bb_saved_builders"].Rows[iIndx]
    > > ["sales_rep_approved"].ToString());

    >
    > > switch(iResultSR)
    > > {
    > > case 0:
    > > img1.ImageUrl="../../images/greybox.gif";
    > > break;
    > > case 1:
    > > img1.ImageUrl="../../images/checkboxyes.gif";
    > > break;
    > > case 2:
    > > img1.ImageUrl="../../images/checkboxno.gif";
    > > break;
    > > case 3:
    > > img1.ImageUrl="../../images/redcheckboxno.gif";
    > > break;
    > > case 4:
    > > img1.ImageUrl="../../images/lock.gif";
    > > break;
    > > default:
    > > img1.ImageUrl="../../images/checkboxno.gif";
    > > break;
    > > }
    > > }
    > > catch(Exception exBinding1)
    > > {
    > > //ignore...
    > > }- Hide quoted text -

    >
    > - Show quoted text -


    Thanks Sergey, after I realized that the line should read
    e.Item.DataItem...everything works great!!!

    Thanks alot...You have been a great help!!!!! :)
    Scott, Aug 9, 2007
    #3
    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. =?Utf-8?B?amF2YXRvcGlh?=

    Dynamic columns not sorting in DataGrid

    =?Utf-8?B?amF2YXRvcGlh?=, Oct 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    496
    =?Utf-8?B?amF2YXRvcGlh?=
    Oct 2, 2004
  2. =?Utf-8?B?RGF2aWQgTWNDb3JtaWNr?=

    DataGrid, Sorting & Checkboxes

    =?Utf-8?B?RGF2aWQgTWNDb3JtaWNr?=, Jan 6, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    376
    =?Utf-8?B?RGF2aWQgTWNDb3JtaWNr?=
    Jan 6, 2005
  3. Replies:
    5
    Views:
    579
  4. rob thomson
    Replies:
    0
    Views:
    254
    rob thomson
    Sep 4, 2003
  5. Navin
    Replies:
    1
    Views:
    674
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page