C# .NET - how we can bind datatable with gridview

Asked By v k on 13-Jul-11 08:49 AM
hello
How we can bind data table with gridview
Reena Jain replied to v k on 13-Jul-11 08:54 AM
Hi,

Here is a simple code for you

protected void Page_Load(object sender, EventArgs e)
{
  string ConnString = "ConnectionString";
  if (!IsPostBack)
  {
  SqlDataAdapter da = new SqlDataAdapter("Select * from emp", ConnString);
  DataTable dt = new DataTable();
  da.Fill(dt);
  GridView1.DataSource = dt;
  GridView1.DataBind();
  
  }
 
}
Hope this will help you
Jitendra Faye replied to v k on 13-Jul-11 08:54 AM

Use this code

DataTable dt = new DataTable();

dt.Columns.Add("col1");

dt.Columns.Add("col2");



foreach (GridViewRow row in GridView1.Rows)

{

string col1 = row[0].Tostring();

string col2 = row[1].Tostring();

DataRow dr = dt.NewRow();

dr[0] = col1;

dr[1] = col2;

dt.Rows.Add(dr);


}



Now you can use DataTable.

Try and let me know.
TSN ... replied to v k on 13-Jul-11 08:55 AM
hi..

if Datatable  is  dt

gridview.DataSource=dt;
gridview.DataBind();
dipa ahuja replied to v k on 13-Jul-11 08:55 AM
Untitled document
protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
     DataTable dt = new DataTable();
 
     //Adding columns
 
     dt.Columns.Add("Names", typeof(string));
     
    //Adding Row
     DataRow dr = dt.NewRow();
     dr["Names"] = "dipa";
     dt.Rows.Add(dr);
 
     dr = dt.NewRow();
     dr["Names"] = "priya";
     dt.Rows.Add(dr);
 
     GridView1.DataSource = dt;
     GridView1.DataBind();
   }
}
v k replied to Reena Jain on 13-Jul-11 08:56 AM
yes your code is right but i want to bind the value  with grid view  which enter in textbox without first stored in database
can u tell me how can this possible
Ravi S replied to v k on 13-Jul-11 08:56 AM
HI

try this


C# Code to Create ASP.Net DataTable

 

// Initialize a DataTable

DataTable myDataTable = new DataTable();

 

// Initialize DataColumn

DataColumn myDataColumn = new DataColumn();

 

// Add First DataColumn

// AllowDBNull property

myDataColumn.AllowDBNull = false;

 

// set AutoIncrement property to true

myDataColumn.AutoIncrement = true;

 

// set AutoIncrementSeed property equal to 1

myDataColumn.AutoIncrementSeed = 1;

 

// set AutoIncrementStep property equal to 1

myDataColumn.AutoIncrementStep = 1;

 

// set ColumnName property to specify the column name

myDataColumn.ColumnName = "auto_ID";

 

// set DataType property of the column as Integer

myDataColumn.DataType = System.Type.GetType("System.Int32");

 

// set Unique property of DataColumn to true to allow unqiue value for this column in each row

myDataColumn.Unique = true;

 

// Add and Create a first DataColumn

myDataTable.Columns.Add(myDataColumn);

 

 

 

// Add second DataColumn

// initialize a new instance of DataColumn to add another column with different properties.

myDataColumn = new DataColumn();

myDataColumn.ColumnName = "firstName";

 

// set DataType property of the column as String

myDataColumn.DataType = System.Type.GetType("System.String");

 

// Add and Create a Second DataColumn

myDataTable.Columns.Add(myDataColumn);

 

 

 

// initialize a new instance of DataColumn to add another column with different properties.

myDataColumn = new DataColumn();

myDataColumn.ColumnName = "lastName";

 

// set DataType property of the column as String

myDataColumn.DataType = System.Type.GetType("System.String");

 

// Add and Create a Third DataColumn

myDataTable.Columns.Add(myDataColumn);

 

C# Code to bind ASP.Net GridView to DataTable



GridView1.DataSource = myDataTable;

GridView1.DataBind();

pete rainbow replied to v k on 13-Jul-11 10:41 AM
Simplest level is just assign the data table reference to the DataSource property of the GridView

You can also use the studio designer to both add the gridview and then add a datasource and then assign one of the objects on the datasource to the gridview

you can also create a DataTable at runtime and assign as in case one above

DataTable dt = new DataTable("Table1");
 
dt.Columns.Add("col1",typeof(string));
dt.Columns.Add("col2",typeof(string));
dt.Columns.Add("col3",typeof(int));
 
DataRow dr = dt.NewRow();
 
dr["col1"]= "a";
dr["col2"]= "b";
dr["col3"]= 1;
 
dt.Rows.Add(dr);
 
dt.AcceptChanges();
 
dgView.DataSource = dt;
dgView.DataBind();
Kirtan Patel replied to v k on 13-Jul-11 11:01 AM
Untitled document
/* Bind DataTable To GridView */

public static void FillData()
{
   SqlConnection con = new SqlConnection("Connection String");
   con.Open();
   SqlCommand comm = new SqlCommand("select * from Categories", con);
   SqlDataAdapter da = new SqlDataAdapter(comm);
   DataTable dt = new DataTable();
   da.Fill(dt);
   con.Close();
  GridView1.DataSource = dt;
  DataBind();
}
Reena Jain replied to v k on 13-Jul-11 03:15 PM
Hi,

if you want to add textbox  value to  datagridview, suppose you have DataGridView for example dgv

01  private void GridDesign()
02   {
03   dgv.Columns.Add("column1", "SrNo");
04   dgv.Columns.Add("column2", "name");
05   
06   
07    }

and now you want to add texbox value in that datagridview so write code on button_click()
1   private void button1_Click(object sender, EventArgs e)
2   {
3    dgv.Rows.Add(textBox1.text,textBox2.text);
4   }

To insert data from gridview to database you have to use loop and in loop you can insert row by row data in gridview if you don't want to use bulkcopy
Hope this will help you