Databind formatted data from gridview to new gridview - asp.net

I have a datatable in Gridview1 and have formatted it by adding several new rows in the datatable, I want to push this data to another table to also include the blank rows as part of the data. How would I go about doing this? What I mean is I want to use the data in my first gridview as a datasource.

Add your DataTable on into a Session.
Something like:
DataTable dt = new DataTable();
Session["DataStore"] = dt;
Gridview1.DataSource= dt;
GridView1.Databind();
Then you can use Session["DataStore"] as your datasource .
var dt = (DataTable)Session["DataStore"];
GridView2.DataSource = dt;
GridView2.Databind();
Hipe this Helps
Regards

Related

Sorting dataview from datatable with hidden columns in ASP.NET

I have a gridview in an ASP.NET Web Forms app and I needed to add a way to sort the rows based on 2 columns. I found a way to do that using DataView.
So from my original Gridview I am creating a DataTable and then I do:
DataView dv = new DataView(dt);
dv.Sort = "Column_1, Column_2";
gridView.DataSource = dv;
gridView.DataBind();
Now I can sort the DataView and after that I can add the DataView to the Gridview as a DataSource. This works great, except that my original GridView has some columns that are set to visible=false.
After I do this sorting, the data from the columns that are hidden gets deleted.
Does this happen when I create the DataView from the DataTable or when I do the sort? What could I do to keep the values from the hidden columns?
Before sorting you have to set columns visible=false and after sorting set visible=true will work better below code for sorting:
DataView dv = new DataView(dt);
dv.Sort = "Column_1, Column_2";
gridView.DataSource = dv;
gridView.DataBind();
This works as you have not checked IsPostBack in Page_Load.

How to display a single row in asp.net web forms?

I ask myself how I can display a single row from a datatable object in a control like a gridview.
I already did it with label objects like here: (this is in load event. I already have buttons which increment the zero and decrement)
Tbname.Text = (dset.Tables("coduta").Rows(0).Item("Firma"))
TbStraße.Text = (dset.Tables("coduta").Rows(0).Item("Straße_Firma"))
TbHausnummer.Text = (dset.Tables("coduta").Rows(0).Item("Hausnummer_Firma"))
TbOrt.Text = (dset.Tables("coduta").Rows(0).Item("Ort_Firma"))
the point is I want to show the specific row in something like a gridview control. The only Idea i have is, to create a new table out of the row and that looks like a too complicated way for this. I hope guys can help
cheers steven
I am from C# background but this approach should help you.
Get first row from existing table.
Make clone of existing table.
Add that row to clone table.
Assign that table as datasource for grid
DataRow dr = dset.Tables("coduta").Rows(0);
DataTable dtNew = dset.Tables("coduta").Clone();
dtNew.Rows.Add(dr.ItemArray);
grid.DataSource = dtNew;
grid.DataBind();
try
da = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.SelectCommand = new SqlCommand(#"SELECT * FROM coduta", connString);
da.Fill(ds, "coduta");
dt = ds.Tables["coduta"];
foreach (DataRow dr in dt.Rows)
{
//here is your row of data
}

store datatable into viewstate in asp.net

I've created a DataTable _dt and added columns like below
_dt.Columns.Add("INDATE", typeof(string));
_dt.Columns.Add("INTIME", typeof(string));
_dt.Columns.Add("OUTTIME", typeof(string));
_dt.Columns.Add("OUTDATE", typeof(string));
and I stored _dt into Viewstate:
ViewState["_table"] = _dt;
and then I added a row into that DataTable like this:
DataRow _dr = _dt.NewRow();
_dr[0] = string.Format("{0:dd/MM/yyyy}", System.DateTime.Now);
_dr[3] = string.Format("{0:dd/MM/yyyy}", System.DateTime.Now);
_dt.Rows.Add(_dr);
and bound the DataTable to a gridview successfully.
My problem is: whenever I retrieve the DataTable from Viewstate
DataTable _dtTemp = (DataTable)ViewState["_table"];
the table contains DataRow also...
I don't want the dataRow.. I want table with column field only.
The DataTable object instance stored in the ViewState is the same object instance that you use when adding rows. So, whatever you do with it will be seen when you retrieve it the next time
You could use the Clone method on your original datatable stored in the ViewState and add rows to this clone. In this way you will have two distinct objects and adding rows to the second doesn't affect the first
ViewState["_table"] = _dt;
DataTable temp = _dt.Clone();
DataRow _dr = temp.NewRow();
_dr[0] = string.Format("{0:dd/MM/yyyy}", System.DateTime.Now);
_dr[3] = string.Format("{0:dd/MM/yyyy}", System.DateTime.Now);
temp.Rows.Add(_dr);
....
DataTable temp = (ViewState["_table"] as DataTable).Clone();
....

Cast error on GridView Sorting event

I'm intercepting the RowDatabound event and using:
DbDataRecord record = (System.Data.Common.DbDataRecord)e.Row.DataItem;
to get access to the unbound data source columns. It works fine on page load and when I apply a filter to the dataset. However, when I attempt to initate a sort on a GridView Column, I get :
Error: Unable to cast object of type 'System.Data.DataRowView' to type 'System.Data.Common.DbDataRecord'
I think I've traced the root issue back to the bind method used in the sort:
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
if (eventArgs != null)
{
DataSet SortedData = new DataSet();
ds.Tables[0].DefaultView.Sort = eventArgs.SortExpression + " " + GetSortDirection(eventArgs.SortExpression);
SortedData.Tables.Add(ds.Tables[0].DefaultView.ToTable());
GridView1.DataSource = SortedData;
}
else
{
GridView1.DataSource = ds;
}
GridView1.DataBind();
}
I remember this was a major pain to figure out because only Dataset had a Sort Property
Since you assign a DataSet to GridView1.DataSource, then the type of e.Row.DataItem in the RowDatabound event will be a DataRowView instead of a System.Data.Common.DbDataRecord. You need to change this:
DbDataRecord record = (System.Data.Common.DbDataRecord)e.Row.DataItem;
to this:
DataRowView record = (DataRowView)e.Row.DataItem;
Instead of trying to sort the Dataset, I appended the sort direction and column from the viewstate to the query before binding it to the grid using SqlDataReader. Looks like I went out of my way to make a simple thing hard to begin with...

How to Add Dynamic Column to GridView

I want to add dynamic columns and datatype to gridview in ASP.Net, so I can assign datasource as a data table
Add the columns to the DataTable, populate your data and then assign the DataTable to the DataSource of the gridview.
DataTable workTable = new DataTable("Customers");
DataColumn workCol = workTable.Columns.Add("CustID", typeof(Int32));
workCol.AllowDBNull = false;
workCol.Unique = true;
workTable.Columns.Add("CustLName", typeof(String));
workTable.Columns.Add("CustFName", typeof(String));
workTable.Columns.Add("Purchases", typeof(Double));
Refer: Adding Columns to a DataTable

Resources