1、DataGridView的特点
(1)DataGridView 一次只能显示一个表。如果绑定整个 DataSet,则(DataMember 属性 要设置为表名) 。否则不会显示任何数据。
(2)允许编辑数据。在单元格中双击或按 F2 来修改当前值。
(3)支持自动排序。在列标题中单击该字段,其值按升序或降序对值进行排序。(默认,排序时会按字母或数字顺序进行排序,字母顺序区分大小写)。
(4)支持多种选择,(可以通过单击并拖动来突出显示一个单元格、多个单元格或多个行,左上角的方块可以选择整个表)。
(5)支持自动调整大小,(在标题之间的列分隔符上双击,使左边的列自动按照单元格的内容展开或收缩)。
(6)在处理大量数据时,可以将 VirtualMode 属性设置为 true,以便显示可用数据的子集。
2、绑定数据
使用DataSource属性,将数据绑定到DataGridView。有两种方法,但一般使用第一种。
(1)
DataSet ds = new DataSet();
DataAdapter.Fill(ds, "TableName");
DataGridView1.DataSource = ds.Tables("TableName");
(2)
DataSet ds = new DataSet();
DataAdapter.Fill(Dts, "TableName");
DataGridView1.DataMember = "TableName";
DataGridView1.DataSource = ds;
3、当前单元格的操作
用DataGridView的CurrentCell属性可以 取得或修改当前单元格(及光标所在的单元格)的内容。
(1)取得当前单元格的值
DataGridView1.CurrentCell.Value;
(2)取得当前单元格值的类型
DataGridView1.CurrentCell.ValueType;
(3)取得当前单元格所在的列索引
DataGridView1.CurrentCell.ColumnIndex;
(4)取得当前单元格所在的行 索引
DataGridView1.CurrentCell.RowIndex;
3、行、列的隐藏和删除
(1) 行、列的隐藏
A:隐藏第n列,或“Name”列
DataGridView1.Columns[n-1].Visible = false;
DataGridView1.Columns[“Name”].Visible = false;
B:隐藏第n行,或当前单元格所在的行
DataGridView1.Rows[n-1].Visible = false;
DataGridView1.Rows[DataGridView1.CurrentCell.RowIndex].Visible = false;
(2) 行头、列头的隐藏
A:隐藏列头
DataGridView1.ColumnHeadersVisible = false;
B:隐藏行头
DataGridView1.RowHeadersVisible = false;
(3)删除列
A:删除指定列,如删除“Name”列
DataGridView1.Columns.Remove(“Name”);
B: 删除第n列
DataGridView1.Columns.RemoveAt(n-1);
(4)删除行
A:删除第n行
DataGridView1.Rows.RemoveAt(n-1);
B:删除选中行
foreach (DataGridViewRow row in DataGridView1.SelectedRows)
{
if (!row.IsNewRow)
{
DataGridView1.Rows.Remove(r);
}
}
4、冻结列或行
当某列被冻结(及固定)时,该列左侧的所有列不随着水平滚动条的滚动而移动,同理当某行冻结时,该行上面的所有行不随着垂直滚动条的滚动而移动。
(1)冻结列
DataGridView1.Columns[n-1].Frozen = true;
DataGridView1.Columns[“Name”].Frozen = true;
(2)冻结行
DataGridView1.Rows[n-1].Frozen = true;
5、行、列的操作
(1)限制删除行和添加行
A:
默认,DataGridView 允删除行和添加行操作,但是可以通过将 DataGridView对象的AllowUserToDeleteRows属性和AllowUserToAddRows 属性 设置为false,来禁止用户进行删除行和添加行的操作。
注意:只是不允许在DataGridView的Form上进行删除行和添加行的操作,通过编程的方式(DataGridViewRowCollection.Remove)还是可以添加和删除行的。如果 DataGridView 绑定的是 DataView 的话,通过 DataView.AllowDelete 也可以控制行的删除。
DataGridView1 .AllowUserToDeleteRows=false ;
DataGridView1 .AllowUserToAddRows=false ;
B: 行删除事件
在删除行的时候,会触发 DataGridView.UserDeletingRow 事件,可以在这个事件中添加删除行的判断。如下:
private void DataGridView1_UserDeletingRow( object sender, DataGridViewRowCancelEventArgs e)
{
//确认信息
if (MessageBox.Show("确定要删除该行吗?", "删除确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
{
// 删除行
}
}
(2)添加列
当DataGridView的DataSource属性设定绑定了数据的时候,DataGridView默认自动添加列。DataGridView.AutoGenerateColumns属性设为False时,必须手动才能添加列。
DataGridView1.AutoGenerateColumns = false;//设定列不能自动作成
DataGridView1.DataSource = BindingSource1; // DataSource设定
DataGridViewTextBoxColumn textColumn = new DataGridViewTextBoxColumn(); // DataGridViewTextBoxColumn列作成
textColumn.DataPropertyName = "Column1";
textColumn.Name = "Column1"; //名字
textColumn.HeaderText = "Column1";HeaderDataGridView1.Columns.Add(textColumn); //列追加
(3)添加行
A:
string[] row0 = { "11/22/1968", "29", "Revolution 9", "Beatles", "The Beatles [White Album]" };
string[] row1 = { "4/4/1960", "6", "Fools Rush In", "Frank Sinatra", "Nice 'N' Easy" };
DataGridViewRowCollection rows = this.dataGridView1.Rows;
rows.Add(row0);
rows.Add(row1);
B:string[] row1 = new string[]{"Meatloaf", "Main Dish"};
string[] row2 = new string[]{"Key Lime Pie", "Dessert"};
string[] row3 = new string[]{"Orange-Salsa Pork Chops", "Main Dish"};
object[] rows = new object[] { row1, row2, row3 };
dataGridView.Rows.Add(rowArray);
6、单元格的操作
(1)设置默认值
可以用DataGridView.DefaultValuesNeeded事件,在该事件中可以设置默认值和特定单元格的ReadOnly属性等。
private void DataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
// 设定单元格的默认值
e.Row.Cells["Column1"].Value = 0;
e.Row.Cells["Column2"].Value = "-";
}
(2)对输入值转换
在单元格输入时触发DataGridView.CellParsing事件,可以在该事件中对输入的值进行转换操作。
//将“Column1”列的输入转换为大写
private void DataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.Columns[e.ColumnIndex].Name == "Column1" && e.DesiredType == typeof(string))
{
e.Value = e.Value.ToString().ToUpper(); //将单元格值设为大写
e.ParsingApplied = true; //解析完毕
}
}
(3)自定义单元格显示格式
用CellFormatting事件,可以自定义单元格的显示格式。
private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
// 如果单元格是“Column1”列的单元格
if (dgv.Columns[e.ColumnIndex].Name == "Column1" && e.Value is string)
{
// 设置日期显示格式。
dataGridView1.Columns["Column1"].DefaultCellStyle.Format = "yyyy/MM/dd";
e.FormattingApplied = true;
}
}
CellFormatting事件的DataGridViewCellFormattingEventArgs对象的Value属性一开始保存着未被格式化的值。当Value属性被设定表示用的文本之后,把FormattingApplied属性做为True,告知DataGridView文本已经格式化完毕。如果不这样做的话,DataGridView会根据已经设定的Format,NullValue,DataSourceNullValue,FormatProvider属性会将Value属性会被重新格式化一遍。
(4)为单元格添加工具提示
默认 ,工具提示用于显示因为单元格太小而无法显示其完整内容的DataGridView 单元格的值。但可以重写此行为,以便为各单元格设置工具提示文本值。可以通过将 DataGridView.ShowCellToolTips 属性设置为 false 来禁止显示单元格级的工具提示。
// Sets the ToolTip text for cells in the Rating column.
void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
if ( (e.ColumnIndex == this.dataGridView1.Columns["Rating"].Index)
&& e.Value != null )
{
DataGridViewCell cell =
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (e.Value.Equals("*"))
{
cell.ToolTipText = "very bad";
}
else if (e.Value.Equals("**"))
{
cell.ToolTipText = "bad";
}
else if (e.Value.Equals("***"))
{
cell.ToolTipText = "good";
}
else if (e.Value.Equals("****"))
{
cell.ToolTipText = "very good";
}
}
}7、SelectionMode属性
SelectionMode 属性可以指定的DataGridViewSelectionMode常用属性。
CellSelect 点击单元格时单元格被选择,行或列不能被选择。
ColumnHeaderSelect 列的Header点击时整列被选择,或者点击单元格时单元格被选择。DataGridViewColumnSortMode.Automatic被设定时不能使用。
FullColumnSelect 列Header或者单元格被点击时,整列被选择。DataGridViewColumnSortMode.Automatic被设定时不能使用。
FullRowSelect 行Header或者单元格被点击时,整行被选择。
RowHeaderSelect 行Header被点击时行被选择。或者单元格被点击时单元格被选择。
(1)不能复数行选择
当DataGridView.MultiSelect属性设定为False时,单元格、行、列的复数选择不可能实现了。
// DataGridView1的单元格、行、列不能复数选择
DataGridView1.MultiSelect = false;(2)单元格选择时,整行被全选
DataGridView.SelectionMode属性设定为FullRowSelect时,单元格被选择时,整行会被选择。
//单元格选择时整行被选择
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;以上的两种方法一起使用时,复数行就不能被选择,单元格选择时整得会被选择。
DataGridView1.MultiSelect = false;
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;