简体   繁体   English

对于使用“ DataGridView”的Windows窗体应用程序,如何检查数据源中的值并更改单独单元格的颜色?

[英]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.

相关问题 使用数据源时无法更改datagridview单元格颜色 - Can't change datagridview cell color when using a datasource 如何更改 datagridview 特定的复选框单元格颜色并根据数据库值进行检查? - How can i change datagridview specific checkbox cell color and check based on database value? 我如何更改单元格datagridview的值 - how can i change the value of a cell datagridview 如何更改特定的 datagridview 单元格值? - how can i change specific datagridview cell value? 如何在Windows窗体中使用C#将选定的DateTimePicker值获取到Excel单元格 - How can i get the selected DateTimePicker Value to the Excel cell using C# in Windows Forms 如何根据 Combobox 的值更改 DataGridView 单元格颜色? - How to change DataGridView cell color based on value of Combobox? DataGridView:如何根据数据源值更改单元格颜色 - DataGridView: How to change cell color depending on data source value 当我在C#Windows应用程序中选中复选框时,如何更改datagridview行中的颜色? - how to change the color in datagridview row when i check the checkbox in C# windows applications? 如何使用 C# 在 datagridview Windows 窗体应用程序内的 ComboBox 中设置默认第一个值 - How to Set Default First Value in ComboBox inside the datagridview Windows Forms Application using C# 如何更改DataGridView背景颜色/单元格颜色 - How to change DataGridView Background color/ Cell Color
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM