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

2 Answers 6161 Views
Grid
Jesper
Top achievements
Rank 1
Jesper asked on 14 Feb 2011, 03:48 PM
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 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 15 Feb 2011, 06:47 AM
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.
Sriram
Top achievements
Rank 1
commented on 07 Jun 2018, 05:27 PM

Thanks a ton. This solved my issue of getting the column value in grid.
0
Jesper
Top achievements
Rank 1
answered on 15 Feb 2011, 12:36 PM
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
Tags
Grid
Asked by
Jesper
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Jesper
Top achievements
Rank 1
Share this question
or