Loading...

Using DataGridView in C# Tutorial (Without SQL Database)


DataGridView in Windows Form Application:

DataGridView in used to show data in organized way and to show data from SQL database. The huge use of DataGridView is to show data from SQL database. There are a lot of tutorial on the web about taking data from the SQL database and show it in a DataGridview. A good tutorial about taking data from SQL databases is also given at www.dotnetperls.com. Here we will discuss about adding, deleting or removing and updating data from DataGridView using Text Boxes and without any database.

So here we start our tutorial to Use DataGridView without database. 

  • Open Visual Studio and Windows Form Application
  • Add three Textboxes, three buttons and a DataGridView, also add three columns in DataGridView by going in properties so your Form will look like this:


  • Now we have three Buttons, it means in this tutorial we will cover three function, Adding in DataGridView, Deleting from DataGridView and updating or editing in DataGridView. 

To Add in DataGridView From Textboxes:

You have to write following code to add from textboxes in a DataGridView in the click event of Add Button:


private void btn_add_Click_1(object sender, EventArgs e)
        {
            DataGridView1.Rows.Add(txtBox_name.Text, txtBox_regno.Text, txtBox_contact.Text); // it will add textboxes data in row of DataGridView,
            // txtBox_name.Text in first and the next in second and third columns

        }


To Remove in DataGridView From Textboxes:

We will write the following code, so that user will select a row and click on Remove Button to delete the selected row:

private void btn_remove_Click(object sender, EventArgs e)
        {
           DataGridView1.Rows.Remove(DataGridView1.CurrentRow); // it will remove the current row of DataGridView

        }


To Remove in DataGridView From Textboxes:

We will write the following code in click event of edit button which will take the data from selected column of DataGridView and write them in respective textboxes and remove that row from the DataGridView, so that user will make changes is TextBoxes and Add again.

private void btn_edit_Click(object sender, EventArgs e)
        {
            txtBox_name.Text = DataGridView1.CurrentRow.Cells[0].Value.ToString();  // it will write the selected column cell 0 (which is of name) data in the name textbox

            txtBox_regno.Text = DataGridView1.CurrentRow.Cells[1].Value.ToString(); // it will write the selected column cell 1 (which is of Reg no) data in the reg no textbox

            txtBox_contact.Text = DataGridView1.CurrentRow.Cells[2].Value.ToString();  // it will write the selected column cell 2 (which is of Contact) data in the contact textbox

            DataGridView1.Rows.Remove(DataGridView1.CurrentRow);  // Simply remove the selected row so that user will modify that of textboxes and add it again

        }

Output:



Learn Simple C# Code to send email here
 Hit Like Button if you understood, It helps a lot to keep us Motivated.

6 comments:

Post a Comment

Google+

 
TOP