Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir, In my windows form I used textboxes to add data to datagridview. Data will be filled in first row, in the same time again data will be filled in the second row. I am trying to write some codes for it but not succeeded. I have forgot the sequence. my codes are
C#
public partial class DVdata : Form
    {
        public DVdata()
        {
            InitializeComponent();
        }
        DataTable dt = new DataTable();
        DataRow dr;

        private void button1_Click(object sender, EventArgs e)
        {
            dt.Columns.Add("Column1", typeof(int));
            dt.Columns.Add("Column2", typeof(string));
            dt.Columns.Add("Column3", typeof(string));
            dt.Columns["Column1"].AutoIncrement = true;
            dt.Columns["Column1"].AutoIncrementSeed = 1;
            dt.Clear();
            dataGridView1.DataSource = dt;
            foreach (DataRow row in dt.Rows)
            {
                dt.Rows.Add(null, row["Column2"].ToString(), row["Column2"].ToString());
            }
            dt.Rows.Add(null, textBox1.Text, textBox2.Text);
            dataGridView1.DataSource = dt;
        }

        public void DataBind()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Column1", typeof(int));
            dt.Columns.Add("Column2", typeof(string));
            dt.Columns.Add("Column3", typeof(string));
            dt.Clear();
            dataGridView1.DataSource = dt;
           
        }
    }
} 

I forgot what next?
Posted
Updated 2-Feb-14 3:06am
v2

1 solution

Use direct binding, see http://msdn.microsoft.com/en-us/library/fbk67b6z(v=vs.90).aspx[^]. It may also be simpler to allow the user to enter data direct into the datagridview.
 
Share this answer
 

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