Click here to Skip to main content
15,912,507 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there,

Hope you all are doing good.
I have question, I have a gridview with a column named 'Status' and the value is fetched from database Sql.
There is a button outside Gridview 'Close'. What i want to do is when user clicks on the button it should first check all the records in gridview have their status = closed, even if there is one record with status ='pending' it should generate a popup asking user are 'you sure you want to close'
I hope i have made myself clear. Looking forward to your help.

Many thanks

What I have tried:

Searhed google but could not find anything similar
Posted
Updated 15-Jan-17 11:27am

Check out this sample code:
Label cell; // assumming the cell is a Label control
for (int i = 0; i < Gridview1.Rows.Count; i++)
{
    cell = Gridview1.Rows[i].FindControl("Status") as Label;
    if (cell.Text == "pending") return false
}
return true;
 
Share this answer
 
In opposite to the solution #1 by Peter Leow[^], if gridview object is bounded with datasource, i'd suggest to not follow through the collection of gridview cells, but through the collection of data of gridview[^] datasource[^].

For example:
C#
DataSet ds = (DataSet)gridview1.DataSource;
var pendingdata = ds.Tables[0].AsEnumerable()
	.Where(x=>x.Field<string>("Status")=="pending")
	.ToList();
foreach(DataRow r in pendingdata)
{
	//display message here!
}


For further details, please see:
DataSet Class (System.Data)[^]
DataSets, DataTables, and DataViews[^]
LINQ to DataSet[^]
Querying DataSets (LINQ to DataSet)[^]
Queries in LINQ to DataSet[^]
LINQ to DataSet Examples[^]
 
Share this answer
 
Another quick example for your reference:

C#
protected void btnClose_Click(object sender, EventArgs e)
{
        //loop through the grid rows
        foreach (GridViewRow row in GridView1.Rows)
        {
               //assuming your are using BoundField column for displaying the Status field
               //change the value of index where your Status field resides in the grid's column
               //index starts at 0
               if(row.Cells[index].Text.Trim().ToLower() == "pending"){
                       ClientScript.RegisterStartupScript(typeOf(this), "alert", "alert('Found pending status.')", true);
                       break;
               } 
        }
}


To display a confirmation from the code-behind, then you may refer this example here.
 
Share this answer
 
Comments
Faran Saleem 16-Jan-17 1:00am    
Thanks for the reply but i am getting error here.. it says 'No overload for method 'RegisterStartupScript' takes 1 arguments
Vincent Maverick Durano 16-Jan-17 1:29am    
where's your code?
Faran Saleem 26-Jan-17 2:48am    
With some tweaking to the code you provided i was able to get the desired results.

Thanks
Vincent Maverick Durano 26-Jan-17 7:37am    
awesome! glad to be of help. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900