Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
I have one WindoesForm having "Two textBoxes" ,Save Button And GridView.in this I plan to enter Some Data in Textboxes then click "saveButton" that data will save in Gridview.so please give me logic.

Thanks In Advance
Prasanna
Posted

First Create Datatable

Dim dt As New DataTable

and Then FormLoad

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add("SNo")
dt.Columns.Add("FirstName")
dt.Columns.Add("LastName")

And Save Button
Dim dr As DataRow = dt.NewRow
dr(0) = dt.Rows.Count + 1
dr(1) = txtFirstName.Text
dr(2) = txtLastName.Text
dt.Rows.Add(dr)
DataGridView1.DataSource = dt
I hope it is helpful.
Sheshu
 
Share this answer
 
Comments
Prasanna from Chennai 27-Mar-13 7:17am    
thnks its working fine
kayalasheshu 27-Mar-13 8:58am    
MostWelcome............
C#
int Row = 0;

            //
            // Check whether the user has entered all the values
            //
            if (textBox1.Text != "" & textBox2.Text != "" &
                             textBox3.Text != "" & textBox4.Text != "")
            {

                //
                // Add new row to DataGridView where the values
                                // are to be entered
                //
                dataGridView1.Rows.Add();

                //
                // Get Row number ,
                // it is reduced by two because
                // the first row number is zero, after adding
                                // new row to allready
                // existing one.
                //
                Row = dataGridView1.Rows.Count - 2;

                //
                // Store values from text boxes to DataGridView
                //
                dataGridView1[0,Row].Value = textBox1.Text;
                dataGridView1[1,Row].Value = textBox2.Text;
                dataGridView1[2,Row].Value = textBox3.Text;
                dataGridView1[3,Row].Value = textBox4.Text;
                dataGridView1.Refresh();

                //
                // This is optional
                // Clear text boxes
                //
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
            }

            //
            // If all text boxes are not filled in
            //
            else
            {
                MessageBox.Show("You did not entered values to all text boxes",
                "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }







for 4 textboxes........ CHEERS
 
Share this answer
 
Comments
raj ch 27-Mar-13 6:32am    
Its better to check individual textboxes whether they are empty
navin ks 27-Mar-13 6:36am    
NO NEED. error(if any) is shown in messagebox..
raj ch 27-Mar-13 6:51am    
U gave an and condition. If any one textbox value is null data is not added into gridview

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