Showing Data in DataGridView without using database::

Sometimes we want to see the values entered in the entry form temporarily and without the querying it from database. This can be done easily by the following procedure.

 

Suppose, I want to enter student’s name and roll and after clicking the add button, the name and roll of the student will be shown in the datagridview box. Remember, I am not willing to use the database to show the data in the datagridview box.

 

I am using a desktop application. That’s why I use windows form rather than web page for inputting the values.

I will describe it in several steps for your better understanding.

 

1.       Open your visual studio 2005 and create a new project. Name it meaningfully and place it in a suitable location in your hard drive. Remember, you have to create a windows application.

2.       A blank form will appear and you have to design it with your own.

a.       Since, we want to add just 2 fields, such as a student’s Name and Roll, you have to take 2 text box fields. For understanding which textbox is what for, you should take 2 label also. Place these labels beside the text boxes and name it meaningfully. I named the components as:

                                                               i.      TextBoxà nameTextBox, rollTextBox

                                                             ii.      LabelànameLabel, rollLabel

b.      Now add a DataGridView into the form and name it meaningfully. Do nothing in the properties window for this. Just rename it.I named it as:

                                                               i.      DataGridViewà studentDataGridView

c.       Add a button below the textboxes and select a suitable text on the button for display.  I name it as:

                                                               i.      Buttonà addButton

d.      If you double click on the button, you will be redirect into the coding page of the form.

3.       Now check that, you have to customize the DataGridView field where you want to show the inserted data. To do this, go to your form’s constructor method [public Form1() for me]and create 2data column and add those columns into a table. Table was created as a global variable for accessing it from any method in that class.

a.       DataColumnà nameColumn, rollColumn

b.      DataTableà myTable

4.       Now go into the method of the button click. Into the method, create a new DataRow into the table.

5.       Put the two values of the textboxes into the cells of the new row.

6.       Add the row values in the table.

7.       Now select the table as the data source of the gridview .

I have uploaded the code file both in mediafire and rapidshare. The links are:

Mediafire: 

http://www.mediafire.com/?sharekey=9c43b2fe26275d42111096d429abd360e04e75f6e8ebb871

Rapidshare:

http://rapidshare.com/files/211563479/DataGridViewPractice.rar

 

That’s all I have got here.If you have any query, feel free to ask me. Your comments are very valuable to me.

Comments

Anonymous said…
Fine Post. But would be better if the declarations were added with it.

Popular posts from this blog

Dropbox as SVN Repository

While Loop and Variable Casting in SQL Script