2
Contributors
2
Replies
6
Views
9 Years
Discussion Span
Last Post by sebastian3
0

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
		}
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.