How to Add Dynamic Column to GridView - asp.net

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

Related

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();
....

How to set a gridView column's text depending on other columns which are binded?

I have a GridView in ASP which I use for displaying products in a shopping cart.
I set the source of GridView the ShopDataSet and the columns Name, Price and Quantity are filled automatically.
What I want to achieve is a new column, added by me, which displays the cost of each row i.e. Cost = price * quantity;
How can I do this programatically, without executing a new query on the database?
I have to say that on Price and Quantity I set format like "${0}" for Price and "{0} piece(s)" for Quantity.
You should populate your datasource programmatically.
Get the results in a SqlDataReader and populate a DataTable with the results of the SqlDataReader.
Now create a new DataTable with columns that match the output that you want including Cost in your case. Now iterate through each row of the 1st DataTable and add rows to the new DataTable with the cost calculation.
For Example,
DataTable oldTable = new DataTable();
oldTable .Load(rdr); // where rdr is your SqlDataReader
Now create your new table
DataTable newTable = new DataTable();
DataColumn noteID = new DataColumn("Cost", typeof(string));
newTable.Columns.Add(noteID);
//Add other columns
foreach (DataRow row in oldTable .Rows)
{
DataRow newRow = newTable.NewRow();
newRow["Cost"] = //your calculation
...
...
newTable.Add(newRow);
}
Now set the DataSource of your grid to NewTable

Databind formatted data from gridview to new gridview

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

how to add values at the end of datatable?

I am getting some data in unstructured form. So i want it to be in structured form. So i take datatable in this i have added two columns, i.e. "field" and "value". Now i added first row in data along with the data in these two columns. But the prob is its replacing the old data every time i come again to save the data in this datatable. My code is:
DataTable dt = new DataTable();
dt.Columns.Add("Field", typeof(string));
dt.Columns.Add("Value", typeof(string));
dt.Rows.Add(val.Substring(0, val.IndexOf(":") + 1), val.Substring(val.IndexOf(":") + 1, val.Length - val.IndexOf(":") - 1))
Using DataTable, you have to create a new row before adding it:
DataTable dt = new DataTable();
dt.Columns.Add("Field", typeof(string));
dt.Columns.Add("Value", typeof(string));
var row = dt.NewRow();
dt["Field"] = "FieldName";
dt["Value"] = "Value";
dt.Rows.Add(row);
NewRow() documentation :
You must use the NewRow method to create new DataRow objects with the
same schema as the DataTable. After creating a DataRow, you can add it
to the DataRowCollection, through the DataTable object's Rows
property. When you use NewRow to create new rows, the rows must be
added to or deleted from the data table before you call Clear.
But if you are only handling a key/value pair, why don't you use a Dictionary<string, string> ?
Are u using this in a loop? only the last line should be in the loop otherwise youre instantiating the datatable each loop.

Working with Asp.net GridView without DataBinding

Is it possible to populate asp.net GridView with data and operate on those data without dataBinding, as it is possible with Winforms DataGridView?
You can set the data source to a datatable that you can build up in code with whatever you like.
var table = new DataTable();
table.Columns.Add("Column1");
table.Columns.Add("Column2");
var row = table.NewRow();
row["Column1"] = "test";
row["Column2"] = "test2";
table.Rows.Add(row);
GridView.DataSource = table;
GridView.DataBind();
You can also set a gridview's data source with a list:
var yourList = new List<YourRowStuff>();
get the list from a database query or build it up manually in code....
GridView.DataSource = yourList;
GridView.DataBind();

Resources