jjrr007
asked on
Set One cell in GridView to Red
I wanted to please ask how to set one cell in a gridview- to have a red background. The red background should only appear when the template column (called textbox3) has a value of "N/A"
Have a look at the snippet. I have dataGridView1 on my form,
and in the form load event I call my code. It sets red color for the cell with indexes [1,1] (second row and second column);
You may put same code to some event handler and add any conditions you want.
and in the form load event I call my code. It sets red color for the cell with indexes [1,1] (second row and second column);
You may put same code to some event handler and add any conditions you want.
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("Col1");
DataColumn dc2 = new DataColumn("Col2");
DataColumn dc3 = new DataColumn("Col3");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
DataRow dr1 = dt.NewRow();
DataRow dr2 = dt.NewRow();
DataRow dr3 = dt.NewRow();
dt.Rows.Add(dr1);
dt.Rows.Add(dr2);
dt.Rows.Add(dr3);
this.dataGridView1.DataSource = dt;
// SetAutoScrollMargin style
DataGridViewCellStyle myStyle = new DataGridViewCellStyle();
myStyle.BackColor = Color.Red;
this.dataGridView1.Rows[1].Cells[1].Style = myStyle;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I tried a combination of everyone's post. I think I got close, but couldn't get it to work. I have posted the C# code and asp.net code. I wanted to please ask what do you suggest?
Thanks.
BTW, If possible, the cell should only appear red, when it's being updated.
Thanks.
BTW, If possible, the cell should only appear red, when it's being updated.
C# Code*******************
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow pr = ((DataRowView)e.Row.DataItem).Row;
string id = "textbox3";
if (id == "N/A")
e.Row.Cells[2].ForeColor = System.Drawing.Color.Red;
}
}
}
ASP.NET code is Below*********
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksDWConnectionString %>"
SelectCommand="NewRickID" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
</div>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksDWConnectionString %>"
SelectCommand="NewRick" SelectCommandType="StoredProcedure" UpdateCommand="NewRickUpdate"
UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DefaultValue="999" Name="ProductID" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="ProductNumber" Type="String" />
<asp:Parameter Name="Color" Type="String" />
<asp:Parameter Name="ProductID" Type="Int32" />
<asp:Parameter Name="StartDate" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="ProductID" SortExpression="ProductID">
<EditItemTemplate>
<asp:TextBox ID="txtProductID" runat="server" Text='<%# Bind("ProductID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField HeaderText="Color" SortExpression="Color">
<EditItemTemplate>
<asp:TextBox ID="textbox3" runat="server" Text='<%# Bind("Color") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Color") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StartDate" SortExpression="StartDate">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("StartDate") %>'></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="~/Calendar_scheduleHS.png" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="TextBox1" PopupButtonID="Image1">
</ajaxToolkit:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("StartDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
OK, I have a gridview and a button on mu form
The code in the snippet. If value in the cell = "N/A" I color this cell to red. Run the application and type "N/A" into a cell. I use dataGridView1_CellValueCha nged event for handling this.
60MXG' s code contains some syntax errors.
The code in the snippet. If value in the cell = "N/A" I color this cell to red. Run the application and type "N/A" into a cell. I use dataGridView1_CellValueCha
60MXG' s code contains some syntax errors.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace GridViewTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("Col1");
DataColumn dc2 = new DataColumn("N/A");
DataColumn dc3 = new DataColumn("Col3");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
DataRow dr1 = dt.NewRow();
DataRow dr2 = dt.NewRow();
DataRow dr3 = dt.NewRow();
dt.Rows.Add(dr1);
dt.Rows.Add(dr2);
dt.Rows.Add(dr3);
this.dataGridView1.DataSource = dt;
//set values for two cells
this.dataGridView1.Rows[1].Cells[1].Value = "N/A";
this.dataGridView1.Rows[2].Cells[2].Value = "123";
}
private void setCellRed(int rowIndex, int columnIndex)
{
if (this.dataGridView1.Rows[rowIndex].Cells[columnIndex].Value.Equals("N/A"))
{
// Set style
DataGridViewCellStyle myStyle = new DataGridViewCellStyle();
myStyle.BackColor = Color.Red;
this.dataGridView1.Rows[rowIndex].Cells[columnIndex].Style = myStyle;
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
setCellRed(e.RowIndex, e.ColumnIndex);
}
}
}
ASKER
Thanks for your time. I tried that code and received the two messages below. I wanted to please ask what do you suggest?
Error 1 The type or namespace name 'Form' could not be found (are you missing a using directive or an assembly reference?) path\Default.aspx.cs 11 34
Error 2 The type or namespace name 'DataGridViewCellEventArgs
ASKER
The field should be red when the existing value is N/A when being updated (not the new value). Thanks again!
Ha...
You didn't specify and I didn't ask... I wrote the sample code for windows forms because your zones are C# Programming Language, Microsoft Visual C#.Net, .Net Editors & IDEs, not ASP. So, I guess my code is not exactly what you want...
OK, web GridView control is quite similar and you , probably, may use same approach. TableCell that eventually is a target of setting a color has Styele property. Not sure how to set styles. MSDN has some examples...
You didn't specify and I didn't ask... I wrote the sample code for windows forms because your zones are C# Programming Language, Microsoft Visual C#.Net, .Net Editors & IDEs, not ASP. So, I guess my code is not exactly what you want...
OK, web GridView control is quite similar and you , probably, may use same approach. TableCell that eventually is a target of setting a color has Styele property. Not sure how to set styles. MSDN has some examples...
ASKER
How would you suggest that I change the code that I have? C# can be used in webforms or Windows applications. Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your response. I think I am closer. However, it is still not working. Below is the C# code. The value of the color is in the textbox called TextBox1. Also, the label is called Label1. The error I am generating so far is:
Error 1 The type or namespace name 'GridViewRowEventArgs' could not be found (are you missing a using directive or an assembly reference?) C\...Default.aspx.cs 1
I would really appreciate if you could assist me with this. Thanks again!
Error 1 The type or namespace name 'GridViewRowEventArgs' could not be found (are you missing a using directive or an assembly reference?) C\...Default.aspx.cs 1
I would really appreciate if you could assist me with this. Thanks again!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web.UI.WebControls.Adapters;
using System.Web.UI.WebControls.WebParts;
using System.Windows.Forms;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl = (Label)e.Row.FindControl("Label1");
if ((string)DataBinder.Eval(e.Row.DataItem, "TextBox1") == "N/A")
{
lbl.BackColor = System.Drawing.Color.BlanchedAlmond;
}
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
ASKER
I apologize. I realized that I didn't specify that this was for a webform. To be fair, I have awarded points. Please feel free to try this question (asked properly at):
https://www.experts-exchange.com/questions/23579207/Syntax-Error-in-C-Web-Form.html
Thanks for your time!
https://www.experts-exchange.com/questions/23579207/Syntax-Error-in-C-Web-Form.html
Thanks for your time!
http://www.daniweb.com/forums/thread113079.html