Determine Cell Index from Column Name

D

dev648237923

I have a gridview and I call RowDataBound to change the backcolor of a cell
in the Status colomn based on the contents of a field.
I do that below by specifying the column's index by number (4 on my grid) --
but I'd like to specify it by using the column's name ("Status" in my grid).
How can I get the cell index by specifying a bound column's name?
Thank you

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//4 is the column that has Status -- I need a better way to choose this
column
e.Row.Cells[4].BackColor = System.Drawing.Color.FromName(
DataBinder.Eval(e.Row.DataItem, "StatusColor").ToString());
}
}
 
E

Eliyahu Goldin

You may write a little utility method that will loop through the gridview
Columns collection to find a column with matching name. Then use the column
index to locate the cell in the Row.Cells collection.
 
W

Walter Wang [MSFT]

Hi,

You have two options to achieve that:

1) You could use declarative way to do that without writing code-behind:

<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblStatus" Text='<%# Eval("status")
%>' runat="server" BackColor='<%#
System.Drawing.Color.FromName(Eval("statuscolor").ToString())
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>


2) We can iterate the Cells collection of the Row and get the cell by the
containing DataField name:

<asp:BoundField DataField="status" />

protected void OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GetCellByBoundFieldName(e.Row, "status").BackColor =
Color.FromName(DataBinder.Eval(e.Row.DataItem, "StatusColor").ToString());
}
}

DataControlFieldCell GetCellByBoundFieldName(GridViewRow row, string
fieldName)
{
foreach (DataControlFieldCell cell in row.Cells)
{
BoundField bf = cell.ContainingField as BoundField;
if (bf != null)
{
if (bf.DataField == fieldName)
{
return cell;
}
}
}
return null;
}



Let me know if this answers your question.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Cider

Walter Wang said:
Hi,

You have two options to achieve that:

1) You could use declarative way to do that without writing code-behind:

<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblStatus" Text='<%# Eval("status")
%>' runat="server" BackColor='<%#
System.Drawing.Color.FromName(Eval("statuscolor").ToString())
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>


2) We can iterate the Cells collection of the Row and get the cell by the
containing DataField name:

<asp:BoundField DataField="status" />

protected void OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GetCellByBoundFieldName(e.Row, "status").BackColor =
Color.FromName(DataBinder.Eval(e.Row.DataItem, "StatusColor").ToString());
}
}

DataControlFieldCell GetCellByBoundFieldName(GridViewRow row, string
fieldName)
{
foreach (DataControlFieldCell cell in row.Cells)
{
BoundField bf = cell.ContainingField as BoundField;
if (bf != null)
{
if (bf.DataField == fieldName)
{
return cell;
}
}
}
return null;
}



Let me know if this answers your question.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Is there is no other way to get the value in the gridview cell by field name
than to loop through each cell in the row? I remember in the past (probably
in Windows forms) I was able to
row.cells[DataGrid.Columns.IndexOf("fieldName")].text to get the value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top