![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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()); } } |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
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 (wawang (AT) online (DOT) microsoft.com, remove 'online.') Microsoft Online Community Support ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/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 |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |