Ad

Monday, August 19, 2013

How To Add Data to Gridview Without Using Database Or Can we Present data into databound controls without using of database

In this article i would like to explore the concept of creating a  How To Add Data to Gridview Without Using Database.

Overview:
It can be possible with  Data Table.Generally we can get the data from data base we can represent in any of the databound controls like gridview,form view,list view etc..........



                  In this article i would like to pass the data to any of the controls an that data should be presented to the gridview.For this i would like to consider datatable.

Description:
                   Data Table is a collection of rows and columns

For working of Data Table we need to give an instance................
                      DataTable dt=new DataTable();
Now You need to declare columns also
                      DataColumn dc=new DataColumn("ColumName",DataType");
                                                       (or)
                      DataColumn col1=new DataColumn("ID");
                      col1.DataType=System.Type.GetType("System.String");

We can add to DataTable
                        dt.Columns.Add(dc); 
                                 (or)
                         dt.Columns.Add(col1);
Creating a Row in the DataTable
                             DataRow dr=dt.NewRow();
Fill the Data in the DataRow
                             dr[0]="Hi";
Add the Row into the DataTable
                            dt.Rows.Add(dr);
For Multiple Add of rows at same time:
dt.Rows.Add(new object[]{"Field1","Field2",...});
                                 or
dt.Rows.Add("Field1","Field2");

Program:



using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace datatable
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn();
            dc.ColumnName = "ID";
            dc.DataType = typeof(int);
            dt.Columns.Add(dc);
            DataColumn dc1 = new DataColumn();
            dc1.ColumnName = "Name";
            dc1.DataType = typeof(string);
            dt.Columns.Add(dc1);
            DataColumn dc2 = new DataColumn();
            dc2.ColumnName = "Qualification";
            dc2.DataType = typeof(string);
            dt.Columns.Add(dc2);
            DataRow rec = dt.NewRow();
            rec[0] = textBox1.Text;
            rec[1] = textBox2.Text;
            rec[2] = textBox3.Text;
            dt.Rows.Add(rec);
            dataGridView1.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn();
            dc.ColumnName = "ID";
            dc.DataType = typeof(int);
            dt.Columns.Add(dc);
            DataColumn dc1 = new DataColumn();
            dc1.ColumnName = "Name";
            dc1.DataType = typeof(string);
            dt.Columns.Add(dc1);
            DataColumn dc2 = new DataColumn();
            dc2.ColumnName = "Qualification";
            dc2.DataType = typeof(string);
            dt.Columns.Add(dc2);
            dt.Rows.Add("1", "Veerendra", "B.TEch(M.Tech)");
            dt.Rows.Add("2", "Kumar", "B.TEch");
            dt.Rows.Add("3", "Prudhvi", "B.TEch");
            dataGridView1.DataSource = dt;
        }

       
       
      
    }
}

  Output:

DataTable

 

3 comments:

Unknown said...

this is fine.but i want to try it on wcf it showing error how this is doing with wcf..pls help me

Parvathaneni NagaVeerendra said...

bHUVANESWARI SEND ME A ERROR MESSAGE SO THAT I NEED TO CLARIFY YOUR ERROR.SOON I WILL IMPLEMENT DIFFERENT EXAMPLES ON WCF ALSO.

Unknown said...

(Cannot find column 0).this error i got..dt having all data like the columns details...and at clientside i want to display that in datagridview.
using datarow...this is the program i wrote...
that is the error i got..
let me know how to do with wcf..
thanku