datagridview添加数据(datagridview获取选中行数据)

admin 468 0

各位老铁们,大家好,今天由我来为大家分享datagridview添加数据,以及datagridview获取选中行数据的相关问题知识,希望对大家有所帮助。如果可以帮助到大家,还望关注收藏下本站,您的支持是我们最大的动力,谢谢大家了哈,下面我们开始吧!

一、dataGridView如何添加数据

你只是照搬别人的代码,并没有理解什么是List泛型,以下是你代码的解释,希望对你有帮助。

private void Form1_Load(object sender, EventArgs e)

//使用List<>泛型集合填充DataGridView

List<Student> students= new List<Student>();’=====此行是定义泛型,其中Student是一个自定义的类,你抄代码的时候肯定少抄了关于sutdent的定义

Student hat= new Student("Hathaway","12","Male");‘===实例化student,并赋值。

Student peter= new Student("Peter","14","Male");’====同上

Student dell= new Student("Dell","16","Male");‘====同上

Student anne= new Student("Anne","19","Female");’====同上

students.Add(hat);‘===想泛型List中添加实例化的对象

students.Add(peter);’====同上

students.Add(dell);’====同上

students.Add(anne);’====同上

this.dataGridView1.DataSource= students;‘===给dataGridView赋值。

根据以上代码不难看出student对象应该有名称,年龄,性别三个属性。

二、C# datagridview添加数据问题

把你的控件都标出来,好几个TEXTBOX,不知道你说的是哪个!

第一个问题,点击添加按钮之后回传数据库,或者直接对FORM1的类进行传参,用普通的方法写也行,用构造函数也行,传递一个DATAROW或者是DATATABLE都可以,具体看你自己怎么控制,传递DATAROW的话直接添加到原数据源,然后再BIND()一次,至于序号的事情,BIND之后,在CREATROW事件里对GIRDVIEW1.TABLES[0].ROWS[行树].CELLS[0].TEXT=E.ROWINDEX这样赋值就可以了,也可以在ASPX页面内控制Container.ItemIndex+ 1这样

第二个问题跟第一个问题差不多,直接传参就可以了,构造函数也行!怎么都行!

三、C#winform如何通过Datagridview向数据库增加数据

2、建立一个数据库连接类LinkDataBase。因为数据库操作有很多都是重复性工作,所以写一个类来简化对数据库的操作。

using System.Collections.Generic;

private string connectionString="Data Source=Localhost;Initial Catalog=Test;Integrated Security=True";

private SqlConnection myConnection;

private SqlCommandBuilder sqlCmdBld;

private DataSet ds= new DataSet();

//根据输入的SQL语句检索数据库数据

public DataSet SelectDataBase(string tempStrSQL, string tempTableName)

this.myConnection= new SqlConnection(connectionString);

this.da= new SqlDataAdapter(this.strSQL, this.myConnection);

//返回填充了数据的DataSet,其中数据表以tempTableName给出的字符串命名

//数据库数据更新(传DataSet和DataTable的对象)

public DataSet UpdateDataBase(DataSet changedDataSet, string tableName)

this.myConnection= new SqlConnection(connectionString);

this.da= new SqlDataAdapter(this.strSQL, this.myConnection);

this.sqlCmdBld= new SqlCommandBuilder(da);

this.da.Update(changedDataSet, tableName);

//检索数据库数据(传字符串,直接操作数据库)

public DataTable SelectDataBase(string tempStrSQL)

this.myConnection= new SqlConnection(connectionString);

DataSet tempDataSet= new DataSet();

this.da= new SqlDataAdapter(tempStrSQL, this.myConnection);

//数据库数据更新(传字符串,直接操作数据库)

public int UpdateDataBase(string tempStrSQL)

this.myConnection= new SqlConnection(connectionString);

SqlCommand tempSqlcommand= new SqlCommand(tempStrSQL, this.myConnection);

int intNumber= tempSqlcommand.ExecuteNonQuery();

四、C# 怎么把数组中的数据添加到datagridview中

1、当访问DataTable对象时,请注意它们是按条件区分大小写的。例如,如果一个DataTable被命名为“mydatatable”,另一个被命名为“Mydatatable”,则用于搜索其中一个表的字符串被认为是区分大小写的。

2、如果“mydatatable”存在而“Mydatatable”不存在,则认为该搜索字符串不区分大小写。一个DataSet可以包含数个DataTable对象,它们具有相同的TableName属性值和不同的Namespace属性值。

3、按名称引用表中的列、关系和约束是区分大小写的。因此,一个表中可以存在两个或两个以上名称相同的列、关系或约束。例如,您可以有Col1和col1。在这种情况下,按名称引用某一列就必须完全符合该列名的大小写,否则会引发异常。

4、如果表myTable包含列Col1和列col1,就要以myTable.Columns["Col1"]的形式来按名称引用Col1。

5、而以myTable.Columns["col1"]的形式按名称引用col1。尝试以myTable.Columns["COL1"]的形式来引用其中某列就会产生异常。

6、参考资料来源:百度百科-DataTable

五、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)

六、datagridview怎么循环一条一条添加数据

1、如果你是想一条一条的添加数据并且显示到datagridview里:方法如下:

2、1:写个方法show()里面绑定你的datagridview;

3、2:在页面加载load()方法里调用这个show()方法;

4、3:在你的添加按钮里,获取你页面的textbox的值,然后添加到数据库,如果添加成功调用show()方法,添加失败给出提示。

5、如果你是想通过textbox的值,查询数据库的值,然后再显示到datagridview的话:

6、1:写个方法show()里面绑定你的datagridview;

7、2:在页面加载load()方法里调用这个show()方法;

8、3:在按钮点击事件里,获取texebox的值,然后根据这个条件查询数据库,并用一个集合接受,然后再重新绑定datagridview;

好了,本文到此结束,如果可以帮助到大家,还望关注本站哦!