一、C# 如何使datagridview中的单元格处于可编辑
DataGridView控件在实际应用中非常实用,特别需要表格显示数据时。可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行。假如需要动态为DataGridView控件添加新行,方法有很多种,下面简单介绍如何为DataGridView控件动态添加新行的两种方法:
int index=this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[0].Value="1";
this.dataGridView1.Rows[index].Cells[1].Value="2";
this.dataGridView1.Rows[index].Cells[2].Value="监听";
利用dataGridView1.Rows.Add()事件为DataGridView控件增加新的行,该函数返回添加新行的索引号,即新行的行号,然后可以通过该索引号操作该行的各个单元格,如dataGridView1.Rows[index].Cells[0].Value="1"。这是很常用也是很简单的方法。
DataGridViewRow row= new DataGridViewRow();
DataGridViewTextBoxCell textboxcell= new DataGridViewTextBoxCell();
DataGridViewComboBoxCell comboxcell= new DataGridViewComboBoxCell();
2.DataGridView取得或者修改当前单元格的内容:
当前单元格指的是 DataGridView焦点所在的单元格,它可以通过 DataGridView对象的 CurrentCell属性取得。如果当前单元格不存在的时候,返回Nothing(C#是null)
Console.WriteLine(DataGridView1.CurrentCell.Value);
Console.WriteLine(DataGridView1.CurrentCell.ColumnIndex);
Console.WriteLine(DataGridView1.CurrentCell.RowIndex);
另外,使用 DataGridView.CurrentCellAddress属性(而不是直接访问单元格)来确定单元格所在的
行: DataGridView.CurrentCellAddress.Y
列:DataGridView.CurrentCellAddress.X。这对于避免取消共享行的共享非常有用。
当前的单元格可以通过设定 DataGridView对象的 CurrentCell来改变。可以通过 CurrentCell来设定DataGridView的激活单元格。将 CurrentCell设为 Nothing(null)可以取消激活的单元格。
DataGridView1.CurrentCell= DataGridView1[0, 0];
在整行选中模式开启时,你也可以通过 CurrentCell来设定选定行。
private void button4_Click(object sender, EventArgs e)
int row= this.dataGridView1.CurrentRow.Index+ 1;
if(row> this.dataGridView1.RowCount- 1)
this.dataGridView1.CurrentCell= this.dataGridView1[0, row];
private void button5_Click(object sender, EventArgs e)
int row= this.dataGridView1.CurrentRow.Index- 1;
row= this.dataGridView1.RowCount- 1;
this.dataGridView1.CurrentCell= this.dataGridView1[0, row];
*注意: this.dataGridView的索引器的参数是: columnIndex, rowIndex或是 columnName, rowIndex
DataGridView行的用户删除操作的自定义:
默认时,DataGridView是允许用户进行行的删除操作的。如果设置 DataGridView对象的AllowUserToDeleteRows属性为 False时,用户的行删除操作就被禁止了。
//禁止DataGridView1的行删除操作。
DataGridView1.AllowUserToDeleteRows= false;
但是,通过 DataGridViewRowCollection.Remove还是可以进行行的删除。
补足:如果 DataGridView绑定的是 DataView的话,通过 DataView.AllowDelete也可以控制行的删除。
用户在删除行的时候,将会引发 DataGridView.UserDeletingRow事件。在这个事件里,可以判断条件并取消删除操作。
// DataGridView1的 UserDeletingRow事件
private void DataGridView1_UserDeletingRow( object sender, DataGridViewRowCancelEventArgs e)
if(MessageBox.Show("确认要删除该行数据吗?","删除确认",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question)!= DialogResult.OK)
4.DataGridView行、列的隐藏和删除:
DataGridView1.Columns[0].Visible= false;
DataGridView1.Rows[0].Visible= false;
DataGridView1.ColumnHeadersVisible= false;
DataGridView1.RowHeadersVisible= false;
DataGridView1.Columns.Remove("Column1");
DataGridView1.Columns.RemoveAt(0);
DataGridView1.Rows.RemoveAt(0);
foreach(DataGridViewRow r in DataGridView1.SelectedRows)
设定 DataGridView的 AllowUserToOrderColumns为 True的时候,用户可以自由调整列的顺序。
当用户改变列的顺序的时候,其本身的 Index不会改变,但是 DisplayIndex改变了。你也可以通过程序改变 DisplayIndex来改变列的顺序。列顺序发生改变时会引发 ColumnDisplayIndexChanged事件:
// DataGridView1的ColumnDisplayIndexChanged事件处理方法
private void DataGridView1_ColumnDisplayIndexChanged(object sender,
DataGridViewColumnEventArgs e)
Console.WriteLine("{0}的位置改变到{1}",
e.Column.Name, e.Column.DisplayIndex);
6.DataGridView的右键菜单(ContextMenuStrip):
DataGridView, DataGridViewColumn, DataGridViewRow, DataGridViewCell有 ContextMenuStrip属性。可以通过设定 ContextMenuStrip对象来控制 DataGridView的右键菜单的显示。 DataGridViewColumn的 ContextMenuStrip属性设定了除了列头以外的单元格的右键菜单。 DataGridViewRow的 ContextMenuStrip属性设定了除了行头以外的单元格的右键菜单。DataGridViewCell的 ContextMenuStrip属性设定了指定单元格的右键菜单。
// DataGridView的 ContextMenuStrip设定
DataGridView1.ContextMenuStrip= this.ContextMenuStrip1;
DataGridView1.Columns[0].ContextMenuStrip= this.ContextMenuStrip2;
DataGridView1.Columns[0].HeaderCell.ContextMenuStrip= this.ContextMenuStrip2;
DataGridView1.Rows[0].ContextMenuStrip= this.ContextMenuStrip3;
//单元格的 ContextMenuStrip设定
DataGridView1[0, 0].ContextMenuStrip= this.ContextMenuStrip4;
对于单元格上的右键菜单的设定,优先顺序是:Cell> Row> Column> DataGridView
⇒ CellContextMenuStripNeeded、RowContextMenuStripNeeded事件
利用CellContextMenuStripNeeded事件可以设定单元格的右键菜单,尤其但需要右键菜单根据单元格值的变化而变化的时候。比起使用循环遍历,使用该事件来设定右键菜单的效率更高。但是,在DataGridView使用了DataSource绑定而且是VirtualMode的时候,该事件将不被引发。
//CellContextMenuStripNeeded事件处理方法
private void DataGridView1_CellContextMenuStripNeeded(object sender,
DataGridViewCellContextMenuStripNeededEventArgs e)
DataGridView dgv=(DataGridView)sender;
e.ContextMenuStrip= this.ContextMenuStrip1;
e.ContextMenuStrip= this.ContextMenuStrip2;
else if(dgv[e.ColumnIndex, e.RowIndex].Value is int)
e.ContextMenuStrip= this.ContextMenuStrip3;
同样,可以通过RowContextMenuStripNeeded事件来设定行的右键菜单。
//RowContextMenuStripNeeded事件处理方法
private void DataGridView1_RowContextMenuStripNeeded(object sender,
DataGridViewRowContextMenuStripNeededEventArgs e)
DataGridView dgv=(DataGridView)sender;
//当"Column1"列是Bool型且为True时、设定其的ContextMenuStrip
object boolVal= dgv["Column1", e.RowIndex].Value;
if(boolVal is bool&&(bool)boolVal)
e.ContextMenuStrip= this.ContextMenuStrip1;
CellContextMenuStripNeeded事件处理方法的参数中、「e.ColumnIndex=-1」表示行头、「e.RowIndex=-1」表示列头。RowContextMenuStripNeeded则不存在「e.RowIndex=-1」的情况。
7.DataGridView单元格表示值的自定义:
通过CellFormatting事件,可以自定义单元格的表示值。(比如:值为Error的时候,单元格被设定为红色)
下面的示例:将“Colmn1”列的值改为大写。
private void DataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
DataGridView dgv=(DataGridView)sender;
//如果单元格是“Column1”列的单元格
if(dgv.Columns[e.ColumnIndex].Name=="Column1"&& e.Value is string)
string str= e.Value.ToString();
CellFormatting事件的DataGridViewCellFormattingEventArgs对象的Value属性一开始保存着未被格式化的值。当Value属性被设定表示用的文本之后,把FormattingApplied属性做为True,告知DataGridView文本已经格式化完毕。如果不这样做的话,DataGridView会根据已经设定的Format,NullValue,DataSourceNullValue,FormatProvider属性会将Value属性会被重新格式化一遍。
8.DataGridView用户输入时,单元格输入值的设定:
通过 DataGridView.CellParsing事件可以设定用户输入的值。下面的示例:当输入英文文本内容的时候,立即被改变为大写。
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();
9.DataGridView新加行的默认值的设定:
需要指定新加行的默认值的时候,可以在DataGridView.DefaultValuesNeeded事件里处理。在该事件中处理除了可以设定默认值以外,还可以指定某些特定的单元格的ReadOnly属性等。
// DefaultValuesNeeded事件处理方法
private void DataGridView1_DefaultValuesNeeded(object sender,
e.Row.Cells["Column1"].Value= 0;
e.Row.Cells["Column2"].Value="-";
10.DataGridView设定单元格只读:
如果希望,DataGridView内所有单元格都不可编辑,那么只要:
DataGridView1.ReadOnly= true;此时,用户的新增行操作和删除行操作也被屏蔽了。
如果希望,DataGridView内某个单元格不可编辑,那么只要:
//设置 DataGridView1的第2列整列单元格为只读
DataGridView1.Columns[1].ReadOnly= true;
//设置 DataGridView1的第3行整行单元格为只读
DataGridView1.Rows[2].ReadOnly= true;
//设置 DataGridView1的[0,0]单元格为只读
DataGridView1[0, 0].ReadOnly= true;
DataGridView.EditMode属性被设置为 DataGridViewEditMode.EditProgrammatically时,用户就不能手动编辑单元格的内容了。但是可以通过程序,调用 DataGridView.BeginEdit方法,使单元格进入编辑模式进行编辑。
DataGridView1.EditMode= DataGridViewEditMode.EditProgrammatically;
3)根据条件设定单元格的不可编辑状态
当一个一个的通过单元格坐标设定单元格 ReadOnly属性的方法太麻烦的时候,你可以通过 CellBeginEdit事件来取消单元格的编辑。
private void DataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
DataGridView dgv=(DataGridView)sender;
if(dgv.Columns[e.ColumnIndex].Name=="Column1"&&!(bool)dgv["Column2", e.RowIndex].Value)
二、求代码:GridView 使用模版实现点击编辑按钮,只编辑某一列
单击GridView右上角三角形,展开GridView任务,单击编辑列,然后在选定的字段里:1.如果是BoundField,你可以找到右边有个属性 readonly,设置为true即可,换成代码如下
<asp:BoundField DataField="bzetime" HeaderText="时间" SortExpression="bzetime" ReadOnly="True"/>
如果是TemplateField,可以有多种选择,用两个LABEL,或者把TextBox设置为readonly,代码如下:
<asp:TemplateField HeaderText="编制人" SortExpression="bzr">
<ItemTemplate>//本处为正常显示的代码,例中是LABEL,你可以用TextBox,literal等等,看你需要。
<asp:Label ID="Label2" runat="server" Text='<%# Bind("bzr")%>'></asp:Label>
<EditItemTemplate>//本处为编辑时的代码。
<asp:Label ID="Label1" runat="server" Text='<%# Eval("bzr")%>'></asp:Label>
三、C#中datagridview手动绑定数据后怎么编辑列
在查询数据库,返回结果集时更改查询语句,直接返回中文名称的表。例如:select wordID as'单词ID',wordEnglish as'英文单词', wordChinese as'中文解释' from Word
这样的话绑定数据到DataGradView控件时仅仅使用DataGradView控件的一个属性datagridview1.DataSource足够了!!!(如果是WEB程序后面加datagridview1.DataBind();)
查询的时候最好把顺序排好,这样界面代码的复杂程度大大减少。
根据你的补充,你是为显示了所有列而烦恼吧。
这样搞定: this.datagridview1.Columns[1].Visible= false;
或者这样:this.datagridview1.Columns["userID"].Visible= false;
不想让哪一列显示就更改哪一列的Visible属性值为false即可!(列的标示可以用下标,也可以用数据库查出数据时的列名)
另外你说HeaderText属性,WinForm里的DataGradView当然有这个属性了,它是用来设置列头文字的,你可以设置某列的列头文字为英文,也可以设置成中文,示例如下:
this.datagridview1.Columns[1].HeaderText="UserName";
this.datagridview1.Columns[1].HeaderText="用户名字";
这样解释你该清清楚楚,明明白白了吧~
关于gridview编辑和gridview编辑功能的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。