Please let me know how i can insert datagrid values to datatable.

Please let me know how i can insert datagrid values to datatable.

try the below example and ensure u create a empmaster table in the database master

private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			
			SqlConnection sql=new SqlConnection("server=.;database=master;uid=sa;pwd=sa;");
			SqlCommand sqlcmd=new SqlCommand("select empid,empname,empcode from empmaster where isactive=1",sql);
			SqlDataAdapter sqla=new SqlDataAdapter(sqlcmd);
			DataSet ds= new DataSet();
			sqla.Fill(ds);
			DataGrid1.DataSource=ds;
			DataGrid1.DataBind();
			FillDataTable();

			
		}
		private void FillDataTable()
		{

			DataTable dt=new DataTable();
			DataRow dr;
			//Adding the columns to the datatable
			dt.Columns.Add("EmpName");
			dt.Columns.Add("EmpCode");
			foreach (DataGridItem item in DataGrid1.Items)
			{
				dr=dt.NewRow();
				dr[0]= item.Cells[1].Text;
				dr[1]=item.Cells[2].Text;
				dt.Rows.Add(dr);
			}//ForEach
		}

Thanks for replying.
But as far as i m concerned dr[0]= item.Cells[1].Text; wont work as because i have embedded it in asp:templatecolumn n not in boundcolumn.

Anyways , i have already solved the issue using massive findcontrol() ;) thanks for replying.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.