将文本框值插入gridview [英] inserting textbox values into the gridview
问题描述
朋友我在Asp.net中创建一个表单。
我有1个文本框,1个网格Vew和1个按钮。并设置文本框Textmode =Multiline(用户可以在文本框中添加更多记录)。假设我运行程序并在文本框中键入以下两个输入
ABCD IT 25 1000
PQRS IT 30 2000
现在在按钮上单击我想以表格的形式将文本框中的所有记录添加到数据网格视图行。每个记录插入到gridview的新行中LIke
名称|部门|年龄|促销
____________________________________________
ABCD | IT | 25 | 1000
PQRS | IT | 30 | 2000
Friends i create a form in Asp.net.
i have a 1 text Box , 1 Grid Vew, and 1 Button. and set textbox Textmode="Multiline"(user can add more records in textbox). suppose I run program and type following two inputs into textbox
ABCD IT 25 1000
PQRS IT 30 2000
Now on the button click i want to add all record from text box to Data Grid view row in a form of table . each record insert into new row of gridview LIke
Name | Department | Age | salery
____________________________________________
ABCD| IT | 25 | 1000
PQRS| IT | 30 | 2000
推荐答案
我不能声称它是最优化的代码或方式,但它可以肯定地解决您的问题。我已经尝试过了,我的工作正常。
I can't claim it to be the best optimized code or way but it can resolve your problem for sure. I have tried and it's working fine at my end.
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Department", typeof(string));
dt.Columns.Add("Age", typeof(string));
dt.Columns.Add("Salary", typeof(string));
dt.AcceptChanges();
string[] arrs=txtDemo.Text.Split(new string[]{"\n","\r"},StringSplitOptions.RemoveEmptyEntries); //txtDemo: Multiline TextBox ID
foreach (string arr in arrs)
{
string[] arrCol = arr.Split(' ');
DataRow dr = dt.NewRow();
dr["Name"] = arrCol[0];
dr["Department"] = arrCol[1];
dr["Age"] = arrCol[2];
dr["Salary"] = arrCol[3];
dt.Rows.Add(dr);
}
dt.AcceptChanges();
gv.DataSource = dt; //gv: GridView ID
gv.DataBind();
注意:您需要添加验证输入和输入格式的代码。
希望,它有助于:)
Note: you need to add code for validating input and input format.
Hopefully, it helps :)
http://www.aspsnippets.com/Articles/Add-new-Row-to-GridView-on-Button-Click-in-ASPNet.aspx [ ^ ]
http://www.aspsnippets.com/Articles/Add-new-Row-to-GridView-on-Button-Click -in-ASPNet.aspx [ ^ ]
这篇关于将文本框值插入gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!