How can I export a GridView.DataSource to a datatable or dataset?
You should convert first DataSource
in BindingSource
, look example
BindingSource bs = (BindingSource)dgrid.DataSource; // Se convierte el DataSource
DataTable tCxC = (DataTable) bs.DataSource;
With the data of tCxC
you can do anything.
Exporting a grid view to datatable
The Following Code will help you in adding the Gridview rows to DataTable
DataTable dt = new DataTable();
for (int j = 0; j < grdList.Rows.Count; j++)
{
DataRow dr;
GridViewRow row = grdList.Rows[j];
dr = dt.NewRow();
for (int i = 0; i < row.Cells.Count; i++)
{
dr[i] = row.Cells[i].Text;
}
dt.Rows.Add(dr);
}
How to build a DataTable from a DataGridView?
Well, you can do
DataTable data = (DataTable)(dgvMyMembers.DataSource);
and then use
data.Columns.Remove(...);
I think it's the fastest way. This will modify data source table, if you don't want it, then copy of table is reqired. Also be aware that DataGridView.DataSource
is not necessarily of DataTable
type.
Putting GridView data in a DataTable
you can do something like this:
DataTable dt = new DataTable();
for (int i = 0; i < GridView1.Columns.Count; i++)
{
dt.Columns.Add("column"+i.ToString());
}
foreach (GridViewRow row in GridView1.Rows)
{
DataRow dr = dt.NewRow();
for(int j = 0;j<GridView1.Columns.Count;j++)
{
dr["column" + j.ToString()] = row.Cells[j].Text;
}
dt.Rows.Add(dr);
}
And that will show that it works.
GridView6.DataSource = dt;
GridView6.DataBind();
Filling DataTable from GridView in C#
I solved like this
if (myGridView.Rows.Count > 0)
{
var dt = new DataTable();
dt.Columns.Add("Column1", typeof(string));
dt.Columns.Add("Column2", typeof(Int64));
dt.Columns.Add("Column3", typeof(string));
foreach (GridViewRow row in gd_endYearSchool.Rows)
{
var id = row.Cells[1].Text;
//for find textbox
var tb1 = row.Cells[2].FindControl("tbNr") as TextBox;
int nrord = 0;
if (tb1 != null)
{
var ord = tb1.Text;
if (!Int64.TryParse(ord, out nrord))
{
nrord = 0;
}
}
var text=row.Cell[3].text;
dt.Rows.Add(id,nrord,text);
}
}
how can I set a dataset or datatable row to a value taken from a gridview textbox entry?
Use this code. May be this is what you need.
string txtName = ((TextBox) GridView4.Rows[RowIndex].FindControl("textBox1")).Text; string txtEmail = ((TextBox) GridView4.Rows[RowIndex].FindControl("textBox2")).Text; string txtMobile = ((TextBox) GridView4.Rows[RowIndex].FindControl("textBox3")).Text;
if (txtName != null) { EditT.Tables[0].Rows[RowIndex]["Name"] = txtName; }
if (txtEmail != null) { EditT.Tables[0].Rows[RowIndex]["Email"] = txtEmail; }
if (txtMobile != null) { EditT.Tables[0].Rows[RowIndex]["Mobile"] = txtMobile; }
How to make a DataTable from DataGridView without any Datasource?
Might be a nicer way to do it but otherwise it would be fairly trivial to just loop through the DGV and create the DataTable manually.
Something like this might work:
DataTable dt = new DataTable();
foreach(DataGridViewColumn col in dgv.Columns)
{
dt.Columns.Add(col.Name);
}
foreach(DataGridViewRow row in dgv.Rows)
{
DataRow dRow = dt.NewRow();
foreach(DataGridViewCell cell in row.Cells)
{
dRow[cell.ColumnIndex] = cell.Value;
}
dt.Rows.Add(dRow);
}
Related Topics
"The Semaphore Timeout Period Has Expired" Error for Usb Connection
Foreach Control in Form, How to Do Something to All the Textboxes in My Form
Wpf Binding - Default Value for Empty String
ASP.NET Button Onclick Event Not Firing
Why Doesn't the Xmlserializer Need the Type to Be Marked [Serializable]
Log Off User from Win Xp Programmatically in C#
C# Date Formatting Is Losing Slash Separators
How to Make 'Always-On-Bottom'-Window
What Are the Naming Conventions in C#
How to Center Your Main Window in Wpf
Expose and Raise Event of a Child Control in a Usercontrol in C#
Equivalent Code of Createobject in C#
How to Add Option Groups in ASP.NET Drop Down List
How to Run Commands on Ssh Server in C#