Click here to Skip to main content
16,015,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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.

C#
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 :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900