Working with Asp.net GridView without DataBinding - asp.net

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

Related

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

Get Column Names from SQLDatasource

So I'm using a SQL data source that fires a Store procedure using the selected value of a drop down list as a parameter to return one of a multitude of differing tables. Then I have a gridview that uses this SQLdatasource as it's datasource to display the returned values.
What I want is to be able to get at the column names at run time so that I can use it in a later method. However the SQLDatasource doesn't seem to have a property to get at this data and when I try to get at the columns of the Datagridview after I databind it the gridview only shows a single column (the auto-generated select column). Is there a way to get at this?
I don't know about using the SQLDatasource, but you can do this easily in the codebehind if you load your data into a DataTable. E.g.
var dt = new DataTable();
var connectionString = "your connection string";
using (var db = new SqlConnection(connectionString))
{
var command = new SqlCommand("stored_procedure_name", db);
command.CommandType = CommandType.StoredProcedure;
db.Open();
var reader = command.ExecuteReader();
dt.Load(reader);
}
var columns = dt.Columns;
Each column has a ColumnName property. You can use Linq to get the column names as an array:
var columnNameArray = dt.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToArray();

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

Column in DataView not rendering when databound to DataGrid (or RadGrid)

Using ASP.NET Web Forms.
I am databinding a DataView to a DataGrid with AutoGenerateColumns set to true.
The final column of the DataView is not rendering.
Using the debugger I can see that the column definitely exists in the data grid's datasource.
The column is of type decimal and some of the rows in the column have data in them.
The column has a column name.
The other columns are rendering fine.
What's going on?
Some more info
Funnily enough, I can set AutoGenerateColumns to false, then just add the columns manually:
datagrid.Columns.Clear();
foreach(DataColumn column in dataView.Table.Columns)
{
datagrid.Columns.Add(new BoundColumn {
HeaderText = column.ColumnName });
}
datagrid.DataSource = dataView;
This works fine. Why can't .NET do this with AutoGenerateColumns?
I do something similar using a grid view. Are you setting your data field at all? It doesn't look like it.
for (int i = 0; i < QueryObject.QuerySelectFields.Count; i++)
{
BoundField c = new BoundField();
if (QueryObject.QuerySelectFields[i].DescriptionColumnName != null)
c.DataField = QueryObject.QuerySelectFields[i].DescriptionColumnName;
else
c.DataField = QueryObject.QuerySelectFields[i].ColumnName;
c.HeaderText = QueryObject.QuerySelectFields[i].Description;
c.ItemStyle.Wrap = false;
c.SortExpression = c.DataField;
gvResults.Columns.Add(c);
}

Resources