如何将值从文本框传递到带有循环的datagridview winform C# [英] How to pass values from textbox to datagridview winform C# with loop
问题描述
有4个文本框(txt1,txt2,txt3和txt4)。这4个文本框包含的值如
txt1 = accNo | txt2 =发布者| txt3 =价格| txt4 = qty
500 | Geeta发布者| 350 | 5
我想将文本框值传递给Datagridview,如下所示。
AccNo |出版商|价格
500 | Geeta发布者| 350
501 | Geeta发布者| 350
502 | Geeta发布者| 350
503 | Geeta发布者| 350
AccNo将被添加到DataGridView中5次(在数量中提到)并自动增加1.
请帮忙......
我尝试过的事情:
......................................... ..
尝试
private void button1_Click(object sender,EventArgs e)
{
int accountNo;
双倍价格;
int数量;
string publisher = txtPublisher.Text.Trim();
if(!int.TryParse(txtAccNo.Text.Trim(),out accountNo))
{
MessageBox.Show(请输入有效的账号);
返回;
}
if(!int.TryParse(txtqty.Text.Trim(),out quantity))
{
MessageBox.Show(请输入有效数量);
返回;
}
if(!double.TryParse(txtPrice.Text.Trim(),out price))
{
MessageBox.Show(请输入有效价格);
返回;
}
DataTable dt = new DataTable();
dt.Columns.Add(AccNo,typeof(int));
dt.Columns.Add(Publisher);
dt.Columns.Add(Price,typeof(double));
for(int i = 0; i< quantity; i ++)
{
DataRow row = dt.NewRow();
row [AccNo] = accountNo + i;
row [发布商] =发布商;
行[价格] =价格;
dt.Rows.Add(row);
}
dataGridView1.DataSource = dt;
}
请使用适当的控件命名约定,这将有助于您轻松找到代码中的控件
There are 4 textboxes (txt1, txt2, txt3 & txt4). These 4 textboxes contains values like
txt1=accNo | txt2=Publisher | txt3=Price | txt4=qty 500 | Geeta Publisher | 350 | 5
I want to pass my textbox values into Datagridview as bellow shown.
AccNo | Publisher | Price 500 | Geeta Publisher | 350 501 | Geeta Publisher | 350 502 | Geeta Publisher | 350 503 | Geeta Publisher | 350
AccNo will be added into DataGridView 5 times (mentioned in qty) and auto increase by 1.
Please help....
What I have tried:
...........................................
try
private void button1_Click(object sender, EventArgs e) { int accountNo; double price; int quantity; string publisher = txtPublisher.Text.Trim(); if(!int.TryParse(txtAccNo.Text.Trim(),out accountNo)) { MessageBox.Show("Please enter valid Account Number"); return; } if (!int.TryParse(txtqty.Text.Trim(), out quantity)) { MessageBox.Show("Please enter valid quantity"); return; } if (!double.TryParse(txtPrice.Text.Trim(), out price)) { MessageBox.Show("Please enter valid price"); return; } DataTable dt = new DataTable(); dt.Columns.Add("AccNo", typeof(int)); dt.Columns.Add("Publisher"); dt.Columns.Add("Price", typeof(double)); for (int i = 0; i < quantity; i++) { DataRow row = dt.NewRow(); row["AccNo"] = accountNo + i; row["Publisher"] = publisher; row["Price"] = price; dt.Rows.Add(row); } dataGridView1.DataSource = dt; }
Please use a proper naming convention to the controls which will help you to find the controls in the code behind easily.
这篇关于如何将值从文本框传递到带有循环的datagridview winform C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!