Coming from PHP background. What would be the easiest (best?) way to display a table of data populated in C#? The format of the data could be modified as required, right now, I am thinking 2D list to store the data.
Gridview is nice and all but I haven't found a way to use a C# List as it's data source.
No real feature is required. I can implement my own editor / inserter, etc.
Gridview is what you need. To bind to a list, you just set the list as the datasource and away you go:
List<x> myList = GetList();
myGrid.DataSource = myList;
myGrid.DataBind();
Or use an ObjectDataSource control on the page instead...
DataTable could be another best option
as it can support the specific columns of the table or you can manage the whole table in the DataTable
DataTable dt=new DataTable();
now you can store your table from the database into this and can use it as a data source for your listview or gridview
Related
I am creating a DataTable to store information for my project, which will later push information into a MS Excel file. I want to show the user some of this data in my WinForms project through a DataGridView control, but I do not want to show the user the entire DataTable - just essential columns. I've found this to be a challenge since DataTable is set up to work with rows more than columns.
I've tried to copy across from one DataTable to another, using one for storage and a subset for display, but due to columns sharing references, I either can't have multiple columns with the same name, or I alter the columns in one DataTable which subsequently changes both. I've also tried creating a subset table with DataView, but had the same problems.
Is there a clean way to go about this?
(Otherwise I'll just do it rough and try to set my column sizes so that unnecessary data can't be seen in the DataGridView.)
You need to add the columns (you want to show) in designer view. And set the property AutoGenerateColumns to false
Try this
To hide Columns...
this.DataGridView.Columns[0].Visible = false;
or you could filter the data from one datatable into another...
string[] colnames = new string[] { "col1", "col2" };
dt2 = dt1.DefaultView.ToTable(false, colnames);
I've not tested any of that code....
You could just fill another DataTable selecting only the Columns that you want from you data source....
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.
I need to have a Windows DataGridView that displays data from a C# structure. Does anyone know of any examples for me to learn from? All the examples on Internet are using a SQL or Oracle or Access database. However, I just need to have a C# table kind of structure with few rows and few columns from which to populate my DataGridView.
Thanks
yes you can bind it to IEnumerable items such as lists etc
http://msdn.microsoft.com/en-us/library/y0wfd4yz%28v=vs.90%29.aspx
WinForms DataGridView - databind to an object with a list property (variable number of columns)
A couple of links
DataGridViews can use a locally stored BindingList (http://msdn.microsoft.com/en-us/library/ms132679%28v=vs.110%29.aspx) as the DataSource.
if my understanding of your question.
Yes , this is possible with datatable. Datable rows iterate one by one and directly assign to datagridview.
Here I post one example :
foreach (DataRow rows in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = rows[0].ToString();
dataGridView1.Rows[n].Cells[1].Value = rows[1].ToString();
dataGridView1.Rows[n].Cells[2].Value = rows[2].ToString();
dataGridView1.Rows[n].Cells[3].Value = rows[3].ToString();
}
As other have pointed out, you can either work in unbound, or bound fashion with the DataGridView (DGV).
My preference is to always use bound mode, as this methods allows you to "ignore" the DGV internals (most of the time), and you can then simply change the underlying data to which the grid is bound, and let the Data Binding mechanism of Windows Forms do its job.
When I say that you can igbnore the DGV internals, what I mean is that you don't have to start handling DGV events to set or get your data in and out of it. You jet let Data Binding do it.
As mentioned by others, you can bind a DGV to pretty much anything that implements IEnumerable, so if you have a list of custom objects, the DGV will take that as its datasource.
I hope you have good success with this.
Cheers
I am new to .Net application development. I'm working on a project using MS Access.
In my application I have one Gridview with unbound data. I have one SQL Datasource which I bind to Gridview dynamically. When I bind that SQL Datasource to the Gridview the existing data is overridden. Instead of overriding I want to append the rows to the existing rows in Gridview.
You can't do that. Best fix would be not to use SqlDataSource and get data from db, merge it with existing and bind it to grid.
That was doing the right thing; It definitely overrides the previous data. In that case, you need to create a dataset and append the newly fetched table data to the existing table in the dataset.. :)
You can't.
Gridview can have only one datasource at time. Every time you try to bind another datasource, it always overrides the existing one.
One of the best option is to use JOIN if these datasource are in same database.
Check also:
ASP.NET tips: Display resultset from Multiple DataTable
create a view with multiple data datable
a sample code is below
jv = New JoinView(ds.Tables!Ord, _
"OrderID,CustomerID,EmployeeID,OrderDate,CustOrd.CompanyName Company,CustOrd.ContactName Contact,CustOrd.ContactTitle Position,EmpOrd.FirstName,EmpOrd.LastName", _
"EmployeeID<5", "OrderDate")
DataGrid1.DataMember = ""
DataGrid1.DataSource = jv
for more info go through
http://support.microsoft.com/default.aspx?scid=kb;EN-US;325682
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();