读书人

怪事:datagridview编辑列时移动过列

发布时间: 2012-05-01 12:48:58 作者: rapoo

怪事:datagridview编辑列时,移动过列的位置,现在列索引与实际的对应关系错了
怪事:datagridview编辑列时,移动过列的位置,现在列索引与实际的对应关系错了
我想根据某列的内容,设定表格行的颜色,但是现在不对了
dgr.Cells[12].Value.ToString() == "1"
如上,第12列绝对是呀,但是Cells[12]好像并不是 第12列了(基于0)

C# code
        //高亮显示        private void DataGrid1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)        {            if (e.RowIndex > DataGrid1.Rows.Count - 1)                return;            DataGridViewRow dgr = DataGrid1.Rows[e.RowIndex];            try            {                for (int i = 0; i < DataGrid1.Rows.Count; i++)                {                    //dgr.Cells[12]是状态标志                    if (dgr.Cells[12].Value.ToString() == "1" )                    {                        //dgr.DefaultCellStyle.BackColor = Color.Red;                        dgr.DefaultCellStyle.ForeColor = Color.Blue;                    }                    else if (dgr.Cells[12].Value.ToString() == "2" )                    {                        //冲销标志为0,但是被冲销号不是0,则说明是被冲的记录,令其为绿色                        dgr.DefaultCellStyle.ForeColor = Color.Red;                    }                    if (dgr.Cells[14].Value.ToString() == "√")                    {                        dgr.DefaultCellStyle.BackColor = Color.LightBlue;                    }                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }        }


[解决办法]
那就用Cells["列名"]来判断吧,别用索引了,太死,容易错

读书人网 >C#

热点推荐