How to get (column) values from a radgrid item/row

3 posts, 0 answers
  1. Jesper
    Jesper avatar
    13 posts
    Member since:
    Oct 2010

    Posted 14 Feb 2011 Link to this post

    Hi,
    I have a radgrid with several rows generated from a sql query. I want to update those rows with different statuses (update status column in a database) dependent on the result meaning that rows should be updated with different statuses. Rows in radgrid are selectable (via checkbox) and three buttons triggers three different types of updates (status update). I will try to explain scenario with pseudo code below.

    Select rows you will update to status 1
    Button1 clicked
    {
        Update status = 1 for selected rows in radgrid
        Remove updated/selected rows from radgrid
        Reload grid with remaining rows/items
    }
    Select rows you will update to status 2
    Button2 clicked
    {
        Update status = 2 for selected rows in radgrid
        Remove updated/selected rows from radgrid
        Reload grid with remaining rows/items
    }
    Select rows you will update to status 3
    Button3 clicked
    {
        Update status = 3 for selected rows in radgrid
        Remove updated/selected rows from radgrid
        Reload grid with remaining rows/items
    }

    I'm using C# and asp.net so kindly translate above pseudo code to C# code.

    Any kind of help/advice is highly appreciated!

    Rgds,
    Jesper

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 15 Feb 2011 Link to this post

    Hello Jesper,

    You can achieve this by loops through each selected item in ButtonClick and Update corresponding field in each row.

    C#:

    protected void Button1_Click(object sender, EventArgs e)
    //for updating status=1 for selected record
       {
           foreach (GridDataItem item in RadGrid1.SelectedItems)
           {
               string id = item.GetDataKeyValue("ID").ToString();//get DataKeyValue
               //query to update staus field in db with 'ID'
                
               // sql query to select records from db with status not equal to 1
               RadGrid1.Rebind();
           }
       }

    Use the same logic for each button.

    Hope this helps,
    Princy.

  3. Jesper
    Jesper avatar
    13 posts
    Member since:
    Oct 2010

    Posted 15 Feb 2011 Link to this post

    Hi Princy,
    thx for your answer.

    Your solution didn't work for me. I got an error message that says that Index was out of scope (or to low or something like that...). But I have (after trying and trying...) found a solution that works but requires more rows of code needed than your solution. Since I like solutions with as few rows of code as possible I would ask you whats the difference with my code below and yours? Your code generated the error when come to foreach row/loop so I never came in to the loop.

    foreach(GridDataItem item inRadGrid1.SelectedItems)
    ...

    Below code solved my problem.

    protectedvoidButton1_Click(objectsender, EventArgs e)
    //for updating status=1 for selected record
    {
        foreach(var item in RadGrid1.SelectedItems)
        {
            if (item is GridDataItem)
            {
               GridDataItem dataItem = item as GridDataItem; // found the row (GridDataItem)
               TableCell cellID = dataItem["ID"]; // found the cell (TableCell)
               strId = cellID.Text; // found the cell value (string)
            }      
            //query to update staus field in db with 'ID'
              
            // sql query to select records from db with status not equal to 1
            RadGrid1.Rebind();
        }
        // sql query to select records from db with status not equal to 1
          RadGrid1.Rebind();
    }

    Regards,
    Jesper

Back to Top