[英]For a Windows Forms Application using `DataGridView`, how can I check a value in my DataSource and change the color of a separate cell?
Very specific question, I know. 我知道非常具体的问题。 I'm not sure how best to word this. 我不确定如何最好地表达这一点。 Currently, my cell_formatting
method will change the color of a cell based on its value: 当前,我的cell_formatting
方法将根据其值更改单元格的颜色:
dataGridView.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.cell_formatting);
....
public void cell_formatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
if (dataGridView.Columns[e.ColumnIndex].Name.Equals("LocCode"))
{
if (e.Value.ToString() == "OK")
{
e.CellStyle.BackColor = Color.Red;
}
}
}
What I actually need to do is check a different column index than that of the one I'm changing the color of. 我实际上需要做的是检查与我要更改颜色的列索引不同的列索引。 There's a column color
that will decide what color the LocCode
cell will change to. 有一种列color
将决定LocCode
单元格将更改为哪种颜色。
I imagine there's a way to catch which item in my dataGridView.DataSource
is being looked at while inside cell_formatting()
, but I don't know how to access it. 我想有一种方法可以捕获在cell_formatting()
内的dataGridView.DataSource
哪个项目,但是我不知道如何访问它。
I suggest you to use DataTable to get the color value. 我建议您使用DataTable来获取颜色值。 Here is an simple example for you. 这是一个简单的例子。
I made a Table and add 3 records in it. 我做了一个表,并在其中添加了3条记录。
In form_load, data loaded to DataGridView, 在form_load中,将数据加载到DataGridView中,
DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Server=serverName;Database=db;Trusted_Connection=True");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from TestTable", conn);
dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
dataGridView1.DataSource = dt;
}
Then, here we came cell_formatting event, 然后,我们来到了cell_formatting事件,
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("TestName")) // LocCode
{
if (e.Value != null && e.Value.ToString() != "") // Check for extra line
{
string a = dt.Rows[e.RowIndex]["Color"].ToString(); //current row's Color column value.
e.CellStyle.BackColor = Color.FromName(a); // color as backcolor
}
}
}
Output; 输出;
Hope helps, 希望有帮助,
I imagine there's a way to catch which item in my dataGridView.DataSource is being looked at while inside cell_formatting(), but I don't know how to access it. 我想有一种方法可以捕获在cell_formatting()内的dataGridView.DataSource中的哪个项目,但是我不知道如何访问它。
Sure there is. 当然可以。
First, use the DataGridViewCellFormattingEventArgs.RowIndex
property to get the index of the row being formatted. 首先,使用DataGridViewCellFormattingEventArgs.RowIndex
属性获取要格式化的行的索引。 Then use the index to the get the corresponding DataRow
object, and finally use the DataGridViewRow.DataBoundItem
property to get the corresponding data source object, casting it to the appropriate type: 然后使用索引获取对应的DataRow
对象,最后使用DataGridViewRow.DataBoundItem
属性获取对应的数据源对象,并将其转换为适当的类型:
var item = dataGridView.Rows[e.RowIndex].DataBoundItem as YourDataSourceObjectType;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.