winform DataGridView控件开发经验

1.不让DataGridView控件自动生成列

设置AutoGenerateColumns 为false.

dgTicket.AutoGenerateColumns = false; //将自动生成列禁用

2.DataGridView中RowsAdded事件的注意事项

在ASP.NET中的GridView控件在显示绑定数据时有一个RowDataUpdated事件,在其事件响应函数里可以逐行扫描每行所绑定的数据,根据需要可以修改GridView显示的格式或Value等,比较方便。

而在WinForm工程中,与之类似的DataGridView控件,只有RowsAdded事件与之类似,但与GridView不同的是,RowsAdded事件的响应不像RowDataUpdated那样逐条响应,我在Debug很久才发现这个问题,其实只要看到DataGridViewRowsAddedEventArgs e的属性中的RowCount就明白,RowsAdded事件响应时可能插入多行,也可能插入单行(Rows可是复数形式,笨)。故,使用e.RowIndex获取插入行的索引值,e.RowCount获取插入行的数量,使用这两个变量可以方便地遍历所绑定的数据,代码如下所示:

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)  
{  
    int rowIndex = e.RowIndex;  
    int rowCount = e.RowCount;  
    for (int i = rowIndex; i < rowIndex + rowCount; i++)  
    {  
        //绑定的为List<AccountInfo>数据集,此处获取指定行绑定的数据  
        AccountInfo info = this.dataGridView1.Rows[i].DataBoundItem as AccountInfo; 

        /////////////////////////////////////////  
        // 其他数据操作等  
        ///////////////////////////////////////////  
     }  
}

3.与asp.net RowDataBound 等价的事件是CellFormatting 事件

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
 {
        //bala bala
}

4.获取DataGridViewCheckBoxCell的选中状态

private void butUpdate_Click(object sender, EventArgs e)
        {
            bool IsCheck = false;
            int count = this.dataGridView1.RowCount;
            Merchandise_Info mi = new Merchandise_Info();
            for (int i = 0; i < count; i++)
            {
                //if ((bool)(((DataGridViewCheckBoxCell)this.dataGridView1.Rows[i].Cells[0]).Value) == true)
                if ((bool)(((DataGridViewCheckBoxCell)this.dataGridView1.Rows[i].Cells[0]).EditedFormattedValue) == true)
                {
                    mi.Name = this.dataGridView1.Rows[i].Cells["TypeName"].Value.ToString();
                    IsCheck = true;
                    MessageBox.Show(this.dataGridView1.Rows[i].Cells["TypeName"].Value.ToString());
                }
               
            }
            if (IsCheck == false)
            {
                MessageBox.Show("请先选择要修改的行!", "系统提示");
            }
        }

解释:(DataGridViewCheckBoxCell)this.dataGridView1.Rows[i].Cells[0] //先把行i第0列(行i第0列里放的是CheckBox控件)的转换成DataGridViewCheckBoxCell

(bool)(((DataGridViewCheckBoxCell)this.dataGridView1.Rows[i].Cells[0]).Value) //在转换成bool类型!

最关键的地方是:以下是通过两种方式判断CheckBox的选中状态!

(1)if ((bool)(((DataGridViewCheckBoxCell)this.dataGridView1.Rows[i].Cells[0]).Value) == true)
(2)if ((bool)(((DataGridViewCheckBoxCell)this.dataGridView1.Rows[i].Cells[0]).EditedFormattedValue) == true)

两者的区别是:如果你采用(1)方法,则你必须先给CheckBox控件设初始值(要先把CheckBox的值设为已选或为选或者你要先把CheckBox勾上,总之你要动一下CheckBox或者给他先赋值),否则会出错:“未将对象引用设置到对象的实例。”

如果你采用方法(2)则不用给CheckBox控件设初始值,EditedFormattedValue是获得单元格当前格式化的值,而不考虑当前单元格是否是处于编辑状态,也不论也不论是否尚未提取此值!所以不用给CheckBox控件设初始值。

哈哈!获得是否选中后就可以获得选中行的值了!!

5.设置行高

//设置行高为21px
   this
.dataGridView1.RowTemplate.Height = 21;
  
// 禁止用户改变DataGridView1的所有列的列宽
     DataGridView1.AllowUserToResizeColumns = false;
     //禁止用户改变DataGridView1所有行的行高
     DataGridView1.AllowUserToResizeRows = false;

posted @ 2014-01-13 11:45  code_flyer  阅读(822)  评论(0编辑  收藏  举报