Get the particular value in gridview using RowDataBound in c#.net

Posted by Kavi.Sirius under ASP.NET on 4/26/2012 | Points: 10 | Views : 39316 | Status : [Member] | Replies : 5
hi

i have one problem to get the particular cell value in gridview, i am using RowDataBound method
below is my coding

protected void gvdApproverStatus_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[10].Text == "1")
{
Button btn = (Button)e.Row.FindControl("btnAccept");
btn.Visible = false;
}
}

}
catch (Exception ex)
{ }
}

but i cant to get the cells[10] value ,its shows the null ,
Thanks in Advance..

Regards
Kavi.n



Responses

Posted by: GSM_GSV on: 4/26/2012 [Member] Starter | Points: 25

Up
0
Down
Put this code in your gridview row data bound

c#
DataRowView drv = (DataRowView)e.Row.DataItem;

string catName = Convert.ToString(drv["YorColumnName"]);
if (catName == "1") {
Button btn = (Button)e.Row.FindControl("btnAccept");
btn.Visible = false;
}


vb
Dim drv As DataRowView = DirectCast(e.Row.DataItem, DataRowView)

Dim catName As String = Convert.ToString(drv("YorColumnName"))
If catName = "1" Then
Dim btn As Button = DirectCast(e.Row.FindControl("btnAccept"), Button)
btn.Visible = False
End If


Note :Do not use cell[0], cell[1] etc, instead use the column names to retrieve the data you want.

---------------------------------------
Live the life you've dreamed

Regards
MADHU

Kavi.Sirius, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kavi.Sirius on: 4/26/2012 [Member] Starter | Points: 25

Up
0
Down
Thank u

Regards
Kavi.n

Kavi.Sirius, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: GSM_GSV on: 4/26/2012 [Member] Starter | Points: 25

Up
0
Down
Mark as answer, if you are satisfied with the solution, so that its easy for others to recognize the post as resolved.

---------------------------------------
Live the life you've dreamed

Regards
MADHU

Kavi.Sirius, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 4/27/2012 [Member] Starter | Points: 25

Up
0
Down
This s way for getting gridview cells...
string s1 ="";
string s2="";
foreach (GridViewRow brow in gv.Rows)
{
s1=((System.Web.UI.WebControls.Label)brow.FindControl("lblTimeStamp")).Text;
s2=((System.Web.UI.WebControls.Button)brow.FindControl("btn")).Text;
}

lblTimeStamp and btn are id where u used in design page.....
<asp:Label ID="lblTimeStamp " runat="server" Text="Customer Code" ToolTip="Customer Code"></asp:Label>

Kavi.Sirius, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Devsainii on: 6/6/2013 [Member] Starter | Points: 25

Up
0
Down
Thanks GSM_GSV. It also work for me.

Kavi.Sirius, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response