Have an application i'm coding at the moment, which uses DataGrid's. What i want to do is replace the header text with that from a column within an sql database.. Has anybody tried this or any idea on how it could be achieved?
What you should do.
Fill a DataTable with data using an sql query(Easy and searchable on Google and Stackoverflow).
Say your query is
Select id, Emp_Name from emp;
Provide that Datatable as ItemSource to DataGrid.
Now your columns of grid will be same as that of your sql query. ie. datagrid will have 2 columns (id and Emp_Name)
Related
I have a MSSQL DB, with lot of records. I have a column in my table with serial number (Auto generated). I am populating the DB in grid-view (C#), I want to sort the gridview based on this serial number column while the gridview loads.
Well why don't you pass already sorted data to gridview.just use order by clause in your sql query and pass the query results to gridview which will be already sorted.
select * from tablename order by SerialNumber(Sql syntax)
About the second solution u can use dataview class to sort data before passing to gridview
Working c# ,want to fill datagridview for each row sql query is different ,Given a button to add the row ,so sql query is user specific.But not find way ,join query not worked because after 1st query filled the datagridview ,when next query complete datagridview data refresh.
As performance wise ,what is economical either direct query from sql for each row or get the table as datatable once and query from that table to populate datagridview?enter image description here
In c# I am trying to have column dropdown filtering while using a datatable as the datasource with an unknown number of columns and rows. All example and help I have found refers to a database with known column data but I am dealing with multiple databases and need to have a dynamic column filter. Just like excel or powerpivot would do. So if a database is changed and columns are removed or added it will still work.
Found solution here
AdvancedDataGridView https://adgv.codeplex.com/
I need an example of code or project with one DataGridView Form and one DataTable so I can edit DataGridView cells and then all the changes are automatically commited into the DataTable. Also how can I create DataTable with the same columns with DataGridView other way that manually?
I tried to create DataGridView and DataTable with the same columns manually and then set the DataGridView.DataSource = DataTable, but data saved into the table are null (anyway number of new rows is correct, but values are invalid).
Tried to google it - all the samples and questions are about savind into DataBase, but I don't need a DB, I just need a simple DataTable and DataGridView binding.
Thanks!
There's no way to create a table automatically from the DataGridView columns (EDIT To refine this: There are ways to automatically create database structures - ORM would be a good keyword here - but I guess that's not what you need). The other way round works well: Create the table and assign it to the DataGridView as its data source. Then the grid should show the respective columns.
"Storing" the values in the data table works only if you have "told" the columns, which field of the underlying table they relate to. That is: You need to create the table columns (it has a name), and when you create the DataGridView column, you need to assign the column name to the DataPropertyName property of the column.
Easiest way for you: Create a typed dataset (use the "Add to project" dialog to add a new "DataSet"), manually create a table in the dataset's designer, drag an instance of your dataset to the form and assign this as the data source to your DataGridView. Everything else should appear automatically.
I hope you this links from codeproject might help:
http://www.codeproject.com/Questions/80935/save-datagrid-data-to-database
http://www.codeproject.com/Articles/12846/Auto-Saving-DataGridView-Rows-to-a-SQL-Server-Data
If your DataTable is set as the DataSource for the dataGridView, the contents in the dataGridView will reflect the data in the DataTable and vice versa. Therefore, if you make changes to the dataGridView, those changes are already in the DataTable.
Convert DatagridView To DataTable.
In C#.NET:
DataTable dt = ((DataView)this.dataGridView1.DataSource).Table;
VB.NET:
Dim dt As DataTable = CType(Me.dataGridView1.DataSource,DataView).Table
I added some columns into datagridview by using the following code:
dataGridView1.Columns.Add("name");
dataGridView1.Columns.Add("age");
dataGridView1.Columns.Add("salary");
I have datatable that contains data from stored procedure
select col1,col2,col3 from emp
I know the traditional way to bind datagridview with datatable or dataset ,but the problem that I rebuilt datagridview where I put multi Headers and I merged some headers together so I want way to bind specific datatable column with specific datagridview column like
dataGridView1.column("Name") = dt.column("col1");
You are looking for the DataGridViewColumn.DataPropertyName property.
dataGridView1.Columns["Name"].DataPropertyName = "col1";