简体   繁体   中英

How to transfer data contained in a textbox to a datagridview?

I have 4 textboxes in a windows form application and a gridview and button called 'Add'. All I want is, the data entered in 4 textboxes should be gone to the datagridview's four different columns in the same row as i click Add button. If i clear the text boxes, fill them again and click the Add button again then the data should go to the 2nd row of the gridview.

create a class like the following somewhere in your app

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;
        }

Steps to follow:

1) Upon Binding the DataGrid View, save the DataSource in a Viewstate Variable

2) Define Click event for Add Button In the click event, recall the Viewstate Variable that has the datasource, Cast it as a DataTable. Make a new row of that datatable, assign values to the cells of the new row. Add this new row to the Datatable, Save the dataTable in ViewSate again, Bind the datasource to the DataGrid

That's All~

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

i suggest to use static collection types...

you can define a class for your entry form and foreach textbox a variable, when you click the add button create object of the class, setting all its values with your textboxes, inserting row in grid, and saving all rows data(object of class per gridview row) in a List

you can also use a static datatable... you have to create columns for each of your textfield.. adding row to datatable.. selecting the datasource of your grid...

if you need further help, i am alway here to help you..

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);

    }

dont forget to markup this answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM