For a Windows Forms Application using `DataGridView`, how can I check a value in my DataSource and change the color of a separate cell?

懵懂的女人 提交于 2020-01-17 06:32:10

问题


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:

    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.

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.


回答1:


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.

Sure there is.

First, use the DataGridViewCellFormattingEventArgs.RowIndex property to get the index of the row being formatted. 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:

var item = dataGridView.Rows[e.RowIndex].DataBoundItem as YourDataSourceObjectType;



回答2:


I suggest you to use DataTable to get the color value. Here is an simple example for you.

I made a Table and add 3 records in it.

In form_load, data loaded to 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,

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,



来源:https://stackoverflow.com/questions/39540935/for-a-windows-forms-application-using-datagridview-how-can-i-check-a-value-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!