How to transfer the data contained in a text box to a datagridview?

I have 4 text fields in a windows form and gridview application and a button called Add. All I want, the data entered in 4 text files, should be sent to datagridview four different columns on the same line as the "Add" button. If I remove the text fields, fill them again and click the Add button again, then the data should go to the second row of the grid.

+4
source share
4 answers

create a class like next in your application

public class MyClass { public string field1 { get; set; } public string field2 { get; set; } public string field3 { get; set; } public string field4 { get; set; } } 

inside your form.cs write this,

 public static List<MyClass> lst = new List<MyClass>(); 

in the click event of your add button do this

 private void btnAdd_Click(object sender, EventArgs e) { MyClass obj = new MyClass(); obj.field1 = txt1.Text.Trim(); obj.field2 = txt2.Text.Trim(); obj.field3 = txt3.Text.Trim(); obj.field4 = txt4.Text.Trim(); lst.Add(obj); dataGridView1.DataSource = lst; } 
+3
source

Stages:

1) After binding to the DataGrid view, save the DataSource in the Viewstate variable

2) Define the Click event for the Add button. In the click event, recall the Viewstate variable that has the data source, open it as a DataTable. Create a new row for this datatable, assign values ​​to the cells of the new row. Add this new row to Datatable, save the DataTable to ViewSate again, bind the data source to the DataGrid

What's all ~

0
source

Thanks to Mr. SHAKIR SHABBIR first of all, but as I think there is no viewstate in the windows form application ...

I suggest using static collection types ...

you can define a class for your input form and specify a variable for the text field when you click the add button, create a class object, set all its values ​​to your text fields, insert a row into the grid and save all row data (class object in a gridview row) in the list

you can also use static datatable ... you need to create columns for each of your text field. adding a row to the datatable .. selecting the data source of your grid ...

If you need more help, I am always here to help you.

0
source

use this code:

  namespace WindowsFormsApplication1 { public partial class Form1 : Form { DataSet ds = new DataSet(); SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Dataset;Integrated Security=True"); SqlDataAdapter ds1 = new SqlDataAdapter(); BindingSource bd = new BindingSource(); public Form1() { InitializeComponent(); } private void btnAdd_Click(object sender, EventArgs e) { ds1.InsertCommand = new SqlCommand("INSERT INTO Employee VALUES(@FirstName,@LastName)", con); ds1.InsertCommand.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text; ds1.InsertCommand.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text; con.Open(); ds1.InsertCommand.ExecuteNonQuery(); con.Close(); } private void btndisplay_Click(object sender, EventArgs e) { ds1.SelectCommand = new SqlCommand("Select * from Employee", con); ds.Clear(); ds1.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; bd.DataSource = ds.Tables[0]; //txtFirstName.DataBindings.Add("Text", bd, "FirstName"); //txtLastName.DataBindings.Add("Text", bd, "LastName"); } private void btnPervious_Click(object sender, EventArgs e) { bd.MovePrevious(); update(); records(); } private void btnNext_Click(object sender, EventArgs e) { bd.MoveNext(); update(); records(); } private void btnFirst_Click(object sender, EventArgs e) { bd.MoveFirst(); update(); records(); } private void btnLast_Click(object sender, EventArgs e) { bd.MoveLast(); update(); records(); } private void update() { dataGridView1.ClearSelection(); dataGridView1.Rows[bd.Position].Selected = true; records(); } private void records() { label1.Text = "records" + bd.Position + " of " + (bd.Count - 1); } 

Remember to tag this answer

0
source

Source: https://habr.com/ru/post/1440996/


All Articles