Rafael Adel
Rafael Adel

Reputation: 7759

How to get all values in a certain GridView column?

I want to check if a deleted row has a value in a certain cell that's the last one of its kind in the whole GridView. I know that i can get the value of a cell of a deleted row by this :

protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
   Response.Write(e.Values["img_cat"]);            
}

But don't know how to check that the value inside the img_cat column of the deleted row doesn't exist in any other img_cat cell.

Also, i want to know if this approach is better for checking for the existence of a certain cell value in a database. Or just doing the check in the database directly inside RowDeleted event.

Upvotes: 1

Views: 3327

Answers (2)

lukiffer
lukiffer

Reputation: 11313

As a supplement to @PranayRana's answer, you could also use Linq:

protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
   var val = e.Values["img_cat"];
   if (!(sender as GridView).Cast<GridViewRow>().Any(x => x["img_cat"] == val))
   {
       //It's the last one, do something.
   }
}

Upvotes: 1

Pranay Rana
Pranay Rana

Reputation: 176956

you can write down this code in your event and check

   foreach (GridViewRow row in gvAdminUsers.Rows) {

    if (row.RowType == DataControlRowType.DataRow) {
              //here 2 can be replace by your column index i.e. index of image column 

        if (row.Cells(2).Text == e.Values["img_cat"]) {
            // found inserted user - select it!
            gvAdminUsers.SelectedIndex = iRowCnt;
        }
        iRowCnt += 1;
    }
 }
 if(iRowCnt>1)
  //there are row exists with this value

like this way you can resolve your problem you need to traverse each row of grid to find out value

Upvotes: 1

Related Questions