Subscribe to Subscribers

Forums » .NET » ASP.NET »

How to get ColumnName in RowDataBound in gridview?


Posted Date: 07 Sep 2012      Posted By:: Arul Jesuraj     Member Level: Silver    Member Rank: 2356     Points: 4   Responses: 4



Hi,

I'm binding some value to particular two columns of each row of the gridview in its rowdatabound...

Before binding value to columns, I want to check the columnName. If columnName is that particular column, then I want to bind data.

Now, e.Row.Cells[0].Text=="Data" I use to bind data to that column...

But, I want to do like,

if(e.Row.Cells["ColumnName"]=="ColumnName")
{
e.Row.Cells["ColumnName"].Text="data";
}

How to do this?...

SanAroJesu
sanarojesu@yahoo.com
9620870608




Responses

#687910    Author: Ajatshatru Upadhyay      Member Level: Gold      Member Rank: 17     Date: 07/Sep/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

I don't think it is possible. One work around could be to use some constant with the column name and use it instead of index number in row data bound event.
Something like below:


protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
const int ColumnName = 2;
//now you can use this const as column name
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[ColumnName].Text="data";
}
}


Hope it'll help you.
Regards
Ajatshatru


 
#687916    Author: Asheej T K        Member Level: Diamond      Member Rank: 2     Date: 07/Sep/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

I doubt you will be able to get the column name but if you know the column name you can find the values. So work around will be to check the value and do the operation you wanted to do.

Try something like below and see if you are getting the correct value or not.


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string strVal = ((DataRowView)e.Row.DataItem)["YOUR_COLUMN_NAME"].ToString();
}



Regards,
Asheej T K
Microsoft MVP[ASP.NET/IIS]
DotNetSpider MVM

Dotnet Galaxy


 
#687945    Author: Prasad kulkarni        Member Level: Diamond      Member Rank: 8     Date: 07/Sep/2012   Rating: 2 out of 52 out of 5     Points: 1

i think it is not possible to get column name directly but you can play with column indices. you can check following link it may help you to accomplish your task

http://stackoverflow.com/questions/9349620/how-to-access-a-gridview-column-on-rowdatabound

hope it helps

Thanks
Koolprasd2003
[DotNetSpider MVM]





 
#687963    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 07/Sep/2012   Rating: 2 out of 52 out of 5     Points: 4

Here is a sample code you can see that how the columns are accessed here


void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{

if(e.Row.RowType == DataControlRowType.DataRow)
{
label lbl = (label) e.Row.FindControl("lblHidden");
DropDownList ddl = (label) e.Row.FindControl("ddlID");

if(lbl.Text =="A")
{
ddl.Items.Clear();
ddl.Items.Add("B");
ddl.Items.Add("C")
}
Else if(lbl.Text =="B")
{
ddl.Items.Clear();
ddl.Items.Add("C")
}
}

}


Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM


 
Post Reply
You must Sign In to post a response.

Next : How to bind the root folder and sub folders and files in treeview
Previous : Timeout expired. error
Return to Discussion Forum
Post New Message
Category:

Related Messages

Awards & Gifts
Talk to Webmaster Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2013 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.