An example of what I want. The first column in particular and it's sub rows.
I'm looking to make a gridview in ASP.NET using C# that looks similar to the image above.
I'd like the first column to be manually set since those values will never change.
The part that I'm having trouble with is adding rows under the first column that I can label.
To show again this is what I'd like:
MyHeader SecondHeader
SubHeader1 DBData
SubHeader2 DBData
Subheader3 DBData
Any guidance is appreciated.
How are you getting your data? Are you using a DataSet, DataReader, DataTable, or XML?
You will probably need to build the data. You could do that by getting your data and building a DataTable, or XML, or List, etc. The first column you would statically populate then the remaining columns would be dynamically populated from your datasource. Once you have your data shaped they way you want you would bind that to the Grid.
Related
I have a datatable in code. And the datagridview in the Ui.
In code I have done gridviewName.DataSource = dtTable1
Now in the UI I can see gridview populated with the table data. In the UI gridview I can update the cell values and/or delete the data rows. Upon doing any changes to the gridview, the changes automatically flow back into the data table.
I am bit confused at this point because I had thought it is a 1 way connection from data table into datagridview. Is this 2 way by design? If yes, then subsequently if I want to perform an operation per row, like send an email per row, then it is recommended to iterate over the gridview or the data table?
Yes, the 2 way data flow is by design; it makes creating applications a lot easier. If you don't want your user to edit a grid you make the grid read only, but typically you show data to a user, you let them edit it and save it. That would get a lot more hard work if you made data binding a one way thing the grid is connected to the datatable directly; it doesn't copy the data it finds into its own internal data array
Always loop over the datatable, read it and edit it directly; the grid will update accordingly; always avoid looping over the datagridview. As the embodiment of model-View-controller your code should manipulate the model (datatable) not try and manipulate the model via the view/controller that is intended for the user (the datagridview)
If you add a DataSet type file to your project and design a strongly typed datatable inside it your life gets easier. Your code looks like:
foreach(var r in soneDataset.EmailQueueDataTable){
mailer.Send(r.From, r.To, r.Subject, r.Message, r.RetryAttempts);
}
With a standard datatable everything is done with string column names or worse, ordinal positions and needs casting:
foreach(DataRow r in EmailQueueDataTable.Rows){
mailer.Send((string)r["From"], (string)r["To"], (string)r["Sujbect"], (string)r["Message"], (int)r["RetryAttempts"]);
}
Intellisense won't help you with the column names either; you'll only find out at runtime that I made a typo in Subject
I've set up a simple data bound gridview that is populated via the autogenerated code for winforms. It is filling based off of the dataset I point it at.
I've updated the underlying database it's supposed to be filling off of to add an additional column to a table. However this added column is not appearing in the gridview.
I have deleted the datset and rebound it and can't find an answer online but am probably searching with incorrect terms. Is there a way to refresh the dataset in some way?
How the gridview is being filled is by:
this.xTableAdapter.Fill(this.DBDataSet.tableName);
I imagine there is a simple way to refresh the underlying dataset but cannot for the life of me find what it is.
This link had the answer. I needed to go into the dataset designer view and right click on the get, fill() query in the query area for my table. Doing so allowed me to modify the query that built the dataset, adding in the missing column.
I have a GridView and a SqlDataSource. All binding done in aspx, works correctly and Select calls an SP with parameters.
Now I would like to access the bound data programmatically, in stuctured form, not parsing back it from the UI, using cells or .FindControl or other hacks. I also do not want to reselect the data using my SqlDataSource's Select.
I've debugged the code but can not find any structured data neither in my GridView variable neither in my SqlDataSource variable. Maybe I was browsing the wrong properties or a cast was missing.
Thx in advance
I don't think you can do that. When you bind the data from an SQL DataSource to a Gridview, it fetches the data to display and then that's it. It's gone.
When a GridView updates the data, it uses the key field ID and the text in the GridView to do it. There is no other DataTable or other structure behind the scenes.
I have SQL database table as shown in the image. I want values of column Location_Instance(i.e., the second column in the image) to to displayed as headers of a GridView. Right now I am copying the column the to a ListBox and using the ListBox to be displayed as headers. But I want to display directly from datasource. Can anyone kindly help me on this. Thank you in advance.
One way to do this would be to write some additional code that creates a new DataTable with the columns set how you want, then insert rows how you want to, from the data in the database.
Doing it in SQL is difficult.
See http://msdn.microsoft.com/en-us/library/hfx3s9wd.aspx for how to do this ...
You can not directly set the column's value as header in grid view, first you have to save it at some place eg ViewState or any control
Write a Select Query and Get the data
like
Select * from Tbl where 'Condition'
Fill the data in a datatable (ex: dt).
Then bind data directly to the gridview.
like
Gridview.Datasource = dt;
Gridview.Databind();
Then Directly The headers will be displayed.
If you want to Display Location_Type as Location Type then you have to write SQL Query like this
Select Location_Type as [Location Type] .... from Tbl where 'Condition'
Logically this is not possible. Because in this case you want to show rows of data in a single row with multiple column. SO in case your location instance increases what will you do??
But if the number of location_instance is fixed then there is a work around. But this is also limited. You can show the location instance as grid view row, not as column. You can style the row so that it will look like header.
To know how to convert a column data to a row data check the below link.
http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/
So once you get the data in a row you can bind the data to the grid view easily by using RowDataBound of grid view.
I have C# application that fills a GridView with data from a DataTable object, which is filled from parsing a text file. Now i want to do a master-detail scheme, where the master GridView shows an identifying column from the DataTable for all rows, and the detail GridView shows all the columns for that row.
All the examples I'm finding seem to require a SqlDataSource control (to provide the filtering mechanism), which then requires a connection to a database. I can't find how to provide this information when using only a DataTable data source. What provider would I use, and what would the connection string look like?
Thanks in Advance for any help.
You already have your DataSource (unless the data will change when you click a GridViewRow), so all you need is to get something like a Key from your current GridView, create a new DataSource by loading data from your DataTable with the selected key in your GridView and bind it to your new GridView. I could also suggest that you take a look to the DetailsView control which is designed to do in fact what you are trying to accomplish here (master-details scheme).
Good luck my friend.
No connection string needed. You already have the data in a DataTable, so you should be able to do something along the lines of:
myGridView.DataSource = myDataTable;
myGridView.Databind();