[英]How to add new row on click winforms
I have a winforms application that I am developing, I have hit a dead end. 我有一个正在开发的winforms应用程序,但是我走到了穷途末路。 What I am trying to do is on each "click", add a new row to my
DataTable
with the values input in the form. 我想做的是在每次“单击”时,使用在表单中输入的值向我的
DataTable
添加一个新行。 This Datatable
is the DataSource for my DataGridView
. 该
Datatable
是我的DataGridView
的数据源。 Can someone point me in the right direction on how this can be achieved. 有人可以向我指出如何实现这一目标的正确方向。
Articles I looked at: 我看过的文章:
How to add new row to datatable gridview 如何向数据表GridView添加新行
My code: 我的代码:
private void btnAdd_Click(object sender, EventArgs e)
{
//inserting into order table
DataTable dt = new DataTable();
string articleId = cmbArticle.Text;
string productDescription = txtDesc.Text;
string type = txtType.Text;
string materialType = txtMaterial.Text;
string size = cmbSizes.Text;
string quantity = txtQuantity.Text;
try
{
dt.Columns.Add("Article");
dt.Columns.Add("Description");
dt.Columns.Add("Type");
dt.Columns.Add("Material");
dt.Columns.Add("Size");
dt.Columns.Add("Quantity");
dt.Columns.Add("DateTime");
DataRow dr = dt.NewRow();
//addrows
dr["Article"] = articleId;
dr["Description"] = productDescription;
dr["type"] = type;
dr["Material"] = materialType;
dr["Size"] = size;
dr["Quantity"] = quantity;
dt.Rows.Add(dr);
dgvView.DataSource = dt;
}
catch (Exception ex)
{
}
}
On each click you are creating a new DataTable
which would be with just one row, You need to create DataTable
once and then just keep adding rows to in the click. 每次单击时,您都将创建一个只有一行的新
DataTable
。您只需创建一次DataTable
,然后在单击中不断添加行即可。 Define your DataTable
at class level and then in your event just add a new row to it. 在类级别定义
DataTable
,然后在您的事件中添加一个新行。
DataTable dt = new DataTable(); //at class level
private void Form1_Load(object sender, EventArgs e)
{
CreateDataTableColumns();
//.... your code
}
Then have a method to create table structure, call that method once from your From_Load
event. 然后有一个创建表结构的方法,从
From_Load
事件中调用该方法一次 。
private void CreateDataTableColumns()
{
dt.Columns.Add("Article");
dt.Columns.Add("Description");
dt.Columns.Add("Type");
dt.Columns.Add("Material");
dt.Columns.Add("Size");
dt.Columns.Add("Quantity");
dt.Columns.Add("DateTime");
}
Later add rows to your class level DataTable
in Add
event. 稍后在
Add
事件中将行添加到类级别的DataTable
中。
private void btnAdd_Click(object sender, EventArgs e)
{
string articleId = cmbArticle.Text;
string productDescription = txtDesc.Text;
string type = txtType.Text;
string materialType = txtMaterial.Text;
string size = cmbSizes.Text;
string quantity = txtQuantity.Text;
try
{
DataRow dr = dt.NewRow();
//addrows
dr["Article"] = articleId;
dr["Description"] = productDescription;
dr["type"] = type;
dr["Material"] = materialType;
dr["Size"] = size;
dr["Quantity"] = quantity;
dt.Rows.Add(dr);
dgvView.DataSource = dt;
}
catch (Exception ex)
{
}
}
(I believe you are doing something with the exception object in your catch block, like logging, showing message to user etc) (我相信您正在对catch块中的异常对象执行操作,例如记录日志,向用户显示消息等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.