向datagrid视图添加多行 [英] adding multiple rows to datagrid view
本文介绍了向datagrid视图添加多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过点击按钮添加多行
form1
使用系统;
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
命名空间 WindowsFormsApplication1
{
public partial class Form1:Form
{
public static 列表< employee> lst = new 列表< employee>();
public Form1()
{
InitializeComponent();
}
私有 void button1_Click( object sender,EventArgs e)
{
employee p = new employee();
p.eid = Convert.ToInt32(textBox1.Text);
p.ename = textBox2.Text;
p.edes = textBox3.Text;
Form2 form2 = new Form2();
form2.passThisValuetoDgv(p.eid,p.ename,p.edes);
form2.ShowDialog();
}
private void button2_Click( object sender,EventArgs e)
{
employee p = new employee();
p.eid = Convert.ToInt32(textBox1.Text);
p.ename = textBox2.Text;
p.edes = textBox3.Text;
lst.Add(p);
dataGridView1.DataSource = lst;
}
}
public class employee
{
public int eid {获得跨度>; set ; }
public string ename { get 跨度>; set ; }
public string edes { get 跨度>; set ; }
}
}
form2
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
命名空间 WindowsFormsApplication1
{
public partial class Form2:表单
{
public Form2()
{
InitializeComponent();
}
私有 void Form2_Load( object sender,EventArgs e)
{
}
DataTable dt;
public void passThisValuetoDgv( int value1, string value2, string value3)
{
dt = new DataTable();
dt.Columns.Add( Col1);
dt.Columns.Add( Col2);
dt.Columns.Add( Col3);
dataGridView1.DataSource = dt;
dt.Rows.Add(value1);
dataGridView1.CurrentRow.Cells [ 1 ]。Value = value2;
dataGridView1.CurrentRow.Cells [ 2 ]。Value = value3;
}
}
}
解决方案
你的问题在这里
dt.Rows.Add(value1);
dataGridView1.CurrentRow.Cells [ 1 ]。 value = value2;
dataGridView1.CurrentRow.Cells [ 2 ]。 value = value3;
如果你想通过直接在网格上使用值来添加行有一个看看这个
如何手动将数据添加到datagridview [ ^ ]
我个人更喜欢将行添加到数据表中以后如何:添加行将更容易为该集合做其他逻辑到一个DataTable [ ^ ]
how to add multiple rows by clicking the button
form1
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public static List<employee> lst = new List<employee>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
employee p = new employee();
p.eid = Convert.ToInt32(textBox1.Text);
p.ename = textBox2.Text;
p.edes = textBox3.Text;
Form2 form2 = new Form2();
form2.passThisValuetoDgv(p.eid, p.ename, p.edes);
form2.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
employee p = new employee();
p.eid = Convert.ToInt32(textBox1.Text);
p.ename = textBox2.Text;
p.edes = textBox3.Text;
lst.Add(p);
dataGridView1.DataSource = lst;
}
}
public class employee
{
public int eid { get; set; }
public string ename { get; set; }
public string edes { get; set; }
}
}
form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
DataTable dt;
public void passThisValuetoDgv(int value1, string value2,string value3)
{
dt = new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col2");
dt.Columns.Add("Col3");
dataGridView1.DataSource = dt;
dt.Rows.Add(value1);
dataGridView1.CurrentRow.Cells[1].Value = value2;
dataGridView1.CurrentRow.Cells[2].Value = value3;
}
}
}
解决方案
Your problem is here
dt.Rows.Add(value1); dataGridView1.CurrentRow.Cells[1].value = value2; dataGridView1.CurrentRow.Cells[2].value = value3;
if you want to add rows by using values direct on the grid have a look at this
How can I manually add data to a datagridview[^]
personally I would prefer to add the row to the datatable as it will be easier to do other logic to that collection later How to: Add Rows to a DataTable[^]
这篇关于向datagrid视图添加多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文