How can I fill DataTable from GridView in ASP.NET?
Simply, you can do like this:
BindingSource bindingSource = (BindingSource )yourGridView.DataSource;
DataTable yourDataTable= (DataTable ) bindingSource .DataSource;
Another way, you can do like this:
DataTable yourDataTable = yourGridView.DataSource as DataTable
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);
}
}
you can fill datatable from gridview with foreach
I'll do like this, i think this'll help u.
public void Data_table()
{
Session["Data"] = "";
DataTable dt = new DataTable();
//Add Columns to the datatable
dt.Columns.Add("c1");
dt.Columns.Add("c2");
dt.Columns.Add("c3");
//Define a datarow for the datatable dt
DataRow dr = dt.NewRow();
//Now add the datarow to the datatable
Session["Data"] = dt;
RadGrid1.DataSource = dt;
RadGrid1.Rebind();
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
if (((System.Data.DataTable)(Session["Data"])).Rows.Count.ToString() != "")
{
RadGrid1.DataSource = Session["Data"];
}
}
Thank you..,
Related
I am Not getting text field values into grid view. Only row increases but I do not get the text.
This is the code that I have for the purpose.
DataTable dt1 = new DataTable();
bool flag = false;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridVIEWData();
Gridview1.DataSource = dt1;
Gridview1.DataBind();
}
}
protected void gobtn_Click(object sender, EventArgs e)
{
if (Session["dtInSession"] != null)
dt1 = (DataTable)Session["dtInSession"];
DataRow dr = dt1.NewRow();
dr["Product"] = DropDownList1.SelectedItem;
dr["Size"] = DropDownList2.SelectedItem;
dr["Case"] = casetxt.Text;
dr["Weight"] = TextBox1.Text;
dr["Price"] = TextBox2.Text;
dt1.Rows.Add(dr);
Session["dtInSession"] = dt1;
Gridview1.DataSource = dt1;
Gridview1.DataBind();
}
private void gridVIEWData()
{
dt1.Columns.Add("Product", typeof(string));
dt1.Columns.Add("Size", typeof(string));
dt1.Columns.Add("Case", typeof(string));
dt1.Columns.Add("Weight", typeof(string));
dt1.Columns.Add("Price", typeof(string));
Session["dtInSession"] = dt1;
}
Please Can any one help me
**I hope you looking for something like this**
protected void BindGridview1()
{
DataTable dtt = new DataTable();
DataTable dt = (DataTable)Session["od"];
dtt.Columns.Add("BookingNO", typeof(string));
dtt.Columns.Add("ItemName", typeof(string));
dtt.Columns.Add("Size", typeof(string));
dtt.Columns.Add("Unit", typeof(string));
dtt.Columns.Add("Price", typeof(string));
dtt.Columns.Add("PendingQty", typeof(string));
for (int i = 0; i < dtt.Rows.Count; i++)
{
DataRow dr = dtt.NewRow();
dr["BookingNO"] = string.Empty;
dr["ItemName"] = string.Empty;
dr["Size"] = string.Empty;
dr["Unit"] = string.Empty;
dr["Price"] = string.Empty;
dr["PendingQty"] = string.Empty;
dtt.Rows.Add(dr);
}
dtt = dt;
gvDetails.DataSource = dtt;
gvDetails.DataBind();
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
TextBox BookingNO = (TextBox)gvDetails.Rows[i].FindControl("BookingNO");
TextBox ItemName = (TextBox)gvDetails.Rows[i].FindControl("ItemName");
TextBox Size = (TextBox)gvDetails.Rows[i].FindControl("Size");
TextBox Unit = (TextBox)gvDetails.Rows[i].FindControl("Unit");
TextBox Price = (TextBox)gvDetails.Rows[i].FindControl("Price");
//TextBox DueDate = (TextBox)gvDetails.Rows[i].FindControl("DueDate");
TextBox PendingQty = (TextBox)gvDetails.Rows[i].FindControl("PendingQty");
BookingNO.Text = Session["BookingNO1"].ToString();
ItemName.Text = dt.Rows[i]["ItemName"].ToString();
Size.Text = dt.Rows[i]["Size"].ToString();
Unit.Text = dt.Rows[i]["Unit"].ToString();
Price.Text = dt.Rows[i]["Price"].ToString();
//DueDate.Text = dt.Rows[i]["DueDate"].ToString();
PendingQty.Text = dt.Rows[i]["PendingQty"].ToString();
}
}
I have DataGridView and DataTable, DataTable is assigned as a datasource in DataGridView.
When I change some value in DataTable it is not update on the view.
So How can i achive this
I have tried following things
BindingSource
Refresh()
Demo Code
DataGridView datagrid = new DataGridView();
DataTable dt = new DataTable();
dt.Columns.Add("No");
dt.Columns.Add("Name");
for (int i = 1; i <= 10; i++)
{
DataRow row = dt.NewRow();
row[0] = i;
row[1] = "ABC";
dt.Rows.Add(row);
}
datagrid.DataSource = dt;
Here my above code
When i change some value in DataTable it is not reflect in DataGridView
dt.Rows[0][1] = "XYZ";
So Please help me....
I am able to do this, which works:
private DataTable _dt = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
_dt.Columns.Add("LongText");
DataRow dr = _dt.NewRow();
dr[0] = "One";
_dt.Rows.Add(dr);
dr = _dt.NewRow();
dr[0] = "Two";
_dt.Rows.Add(dr);
dr = _dt.NewRow();
dr[0] = "Three";
_dt.Rows.Add(dr);
dataGridView1.DataSource = _dt;
}
private void button1_Click(object sender, EventArgs e)
{
_dt.Rows[0][0] = "daddy";
}
You need to add your datagrid to your form, like this:
private void Form1_Load(object sender, EventArgs e)
{
DataGridView datagrid = new DataGridView();
DataTable dt = new DataTable();
//add this line
Controls.Add(datagrid);
dt.Columns.Add("No");
dt.Columns.Add("Name");
for (int i = 1; i <= 10; i++)
{
DataRow row = dt.NewRow();
row[0] = i;
row[1] = "ABC";
dt.Rows.Add(row);
}
datagrid.DataSource = dt;
dt.Rows[0][1] = "XYZ";
}
Result:
I want to fill grid view with textbox and dropddown list values recursively on button click.Currently it is binding only once.I am new to ASP.NET.Help would be highly appreciated
protected void btnAdd_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Resource");
dt.Columns.Add("available");
for (int intCnt = 0; intCnt < grd.Rows.Count - 1; intCnt++)
{
if (grd.Rows[intCnt].RowType == DataControlRowType.DataRow)
{
dr = dt.NewRow();
dr["Resource"] = grd.Rows[intCnt].Cells[0];
dr["available"] = grd.Rows[intCnt].Cells[1];
dt.Rows.Add(dr);
}
}
dr = dt.NewRow();
dr["Resource"] = ddlResource.SelectedItem.Text;
dr["available"] = txtavailable.Text;
dt.Rows.Add(dr);
grd.DataSource = dt;
grd.DataBind();
}
Please try this hope it will help
protected void btnAdd_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Resource");
dt.Columns.Add("available");
foreach(GridViewRow row in grd.Rows)
{
dr = dt.NewRow();
dr["Resource"] = row.Cells[0].Text;
dr["available"] =row.Cells[1].Text;
dt.Rows.Add(dr);
}
dr = dt.NewRow();
dr["Resource"] = ddlResource.SelectedItem.Text;
dr["available"] = txtavailable.Text;
dt.Rows.Add(dr);
grd.DataSource = dt;
grd.DataBind();
}
Use following code:
protected void btnAdd_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = defineColumn();
DataRow dr;
foreach (GridViewRow grow in grdChMedicine.Rows)
{
dr = dt.NewRow();
dr["Diagnosis"] = grow.Cells[1].Text;
dr["DiagnosisId"] = grow.Cells[2].Text;
dt.Rows.Add(dr);
}
dr = dt.NewRow();
dr["Diagnosis"] = ddldiagnosis.SelectedItem.ToString();
dr["DiagnosisId"] = ddldiagnosis.SelectedValue;
dt.Rows.Add(dr);
ViewState["ChMedicine"] = dt;
grdChMedicine.DataSource = dt;
grdChMedicine.DataBind();
}
private DataTable defineColumn()
{
DataTable dt = new DataTable();
dc = new DataColumn("Diagnosis");
dt.Columns.Add(dc);
dc = new DataColumn("DiagnosisId");
dt.Columns.Add(dc);
return dt;
}
I have DataGridView that i am adding items via DataTable
This items read from XML file and inside this XML file,
those items not sorted and i want to sort is before add to my DataGridView
private void UpdateDataGdirView(List<Vendor> list)
{
DataTable dt = new DataTable();
dt.Columns.Add("Column1", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column3", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column4", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column5", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column6", System.Type.GetType("System.Boolean"));
DataRow dr;
foreach (Vendor vendor in list)
{
dr = dt.NewRow();
dr["Column1"] = vendor.IsVendorChecked;
dr["Column2"] = vendor.Number;
dr["Column3"] = vendor.Name;
dr["Column4"] = vendor.Size;
dr["Column5"] = vendor.Path;
dr["Column6"] = vendor.Path2;
dt.Rows.Add(dr);
}
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
this.Invoke((MethodInvoker)delegate { dataGridView1.DataSource = dt; });
}
you can use
dt.defaultview.sort="columnname asc/desc"
and then bind the datatable to its destination.
I think you should use DataView for this. Like :
DataTable orders = dataSet.Tables["SalesOrderHeader"];
EnumerableRowCollection<DataRow> query =
from order in orders.AsEnumerable()
orderby order.Field<decimal>("TotalDue")
select order;
DataView view = query.AsDataView();
bindingSource1.DataSource = view;
Follow this link : Query on DataView
OR
You can use like :
DataTable orders = dataSet.Tables["SalesOrderHeader"];
DataView dv = new DataView(orders);
dv.Sort = "TotalDue";
dataGridView1.DataSource = dv;
I am new in C#.I want to add rows in a GridView in runtime. I collect a data from 2 or 3 tables. But whenever I am going to
bind() it with GridView, the last inserted row is overwritten by current one. And GridView shows only the current row.
Is it possible to show both rows one bellow the other? Or Is there any code for doing so.Please suggest me code for that so that i can use it in my project.Thanks.
Answer::First you have to declare a static datatable.And a boolean variable having value initially "true".
And then execute following code--->>>
Here is My code::
protected void btnAdd_Click(object sender, EventArgs e)
{
int coursemasterid = Convert.ToInt32(dlAdmissionCourses.SelectedItem.Value);
int batchmasterid = Convert.ToInt32(dlAssignBatch.SelectedItem.Value);
string SQL1 = "SELECT coursename,coursefees,batchname FROM CourseMaster,BatchMaster WHERE CourseMaster.coursemasterid=BatchMaster.coursemasterid and CourseMaster.coursemasterid="+coursemasterid+" and BatchMaster.batchmasterid="+batchmasterid+"";
DataTable otable = new DataTable();
otable = DbHelper.ExecuteTable(DbHelper.CONSTRING, CommandType.Text, SQL1, null);
DataRow dr1 = otable.Rows[0];
string coursename = dr1["coursename"].ToString();
int coursefees = Convert.ToInt32(dr1["coursefees"]);
string batchname = dr1["batchname"].ToString();
if (chkadd == true)
{
dtglb = new DataTable(); //here dtglb is a global datatable
dtglb.Columns.Add("coursename", typeof(string));
dtglb.Columns.Add("coursefees", typeof(int));
dtglb.Columns.Add("batchname", typeof(string));
}
foreach (DataRow dr in otable.Rows)
{
dtglb.NewRow();
dtglb.Rows.Add(coursename,coursefees,batchname);
}
chkadd = false;
GridView1.DataSource = dtglb;
GridView1.DataBind();
}
//declaring a datatable global in form
DataTable dtglb=new DataTable();
//In click event
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=EMS;User ID=sa;Password=sa123");
string SQL1 = "SELECT coursename,coursefees,batchname FROM CourseMaster,BatchMaster WHERE CourseMaster.coursemasterid=BatchMaster.coursemasterid and CourseMaster.coursemasterid="+coursemasterid+" and BatchMaster.batchmasterid="+batchmasterid+"";
SqlCommand cmd = new SqlCommand(SQL1, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable ds = new DataTable();
//DataColumn faculty = new DataColumn();
da.Fill(ds);
GridView1.DataSourceID = null;
//New Code Added Here
DataRow row = ds.NewRow();
//your columns
row["columnOne"] = valueofone;
row["columnTwo"] = valueoftwo;
dtglb.Rows.Add(row);
foreach(DataRow dr in dtglb.Rows)
{
ds.Rows.Add(dr);
}
//=========
GridView1.DataSource = ds;
GridView1.DataBind();
add rows to DataGridView itself
DataGridViewRow row = new DataGridViewRow();
dataGridView1.BeginEdit();
//your columns
row.Cells["columnOne"] = valueofone;
row.Cells["columnTwo"] = valueoftwo;
dataGridView1.Rows.Add(row);
dataGridView1.EndEdit();