column header text changes after change DataSource? - c#

When I change DataSource of my DataGridView with DataTable who don't have rows, and after that change DataSource with DataTable who contains rows, header text changes and another properties like width...

Try using a BindingSource control between the DataGridView and the DataTable. The BindingSource control has a very useful property called Filter that will filter out unwanted rows without creating a new DataTable object or otherwise affecting the structure of the underlying DataTable, thus your DataGridView's headers will not be affected.
You can either do this manually through code or drop an instance of the BindingSource in the designer. The code would look something like this:
BindingSource bs = new BindingSource();
bs.DataSource = YourDataTable;
YourGridView.DataSource = bs;
Then you can filter out your results by simply doing:
bs.Filter = "some_column = 'some_value'";

Try clearing your DGV first before re-populating it with a DataTable that has rows.

I solve the problem by making copy of DT and clearing rows from her. Than I assign that DT to DataSorce and now all works fine. Maybe not best way but it works. Thank you all!

Related

How to change column properties of DataGridView which is (datatable bounded at runtime)

Columns are added in my datagridview at runtime.. That is why I Am unable to edit columns in design view and change their properties like i want my ItemName Column AutoSizeMode to Fill..
How can i access such properties in coding???
My database code is...
public void LoadData()
{
con = new SqlConnection("Server=.; database=STORE MANAGEMENT SYSTEM; Trusted_Connection=True;");
SqlDataAdapter sda = new SqlDataAdapter("SELECT tbl_ItemDetail.ItemCode, tbl_ItemDetail.ItemName, tbl_Stock.Quantity, tbl_ItemDetail.Price, tbl_ItemDetail.Category, tbl_ItemDetail.Size, tbl_ItemDetail.Brand FROM tbl_ItemDetail, tbl_Stock WHERE tbl_ItemDetail.ItemCode=tbl_Stock.ItemCode", con);
dt = new DataTable();
sda.Fill(dt);
dataGridView_CRUD.DataSource = dt;
}
DataGridView Image at RunTime...
The simplest way is to directly access the columns after you actually write the .DataBind();, like this, you can access and set any properties you want, like this:
dataGridView_CRUD.Columns[0].Name = "Recipe";
DataGridView.Columns Property
DataGridViewColumn Class
You can also use GridView.RowDataBound Event, to set properties of particular rows and cells in the rows.
This event is invoked upon each single row binding.
GridView.RowDataBound Event
The above is very powerful for your future changes as well, however If this does not suit your needs and you precisely just want to alter a column, then the
#Jimi Answer solved my problem...
"You can use the Designer. Assign the names of the DataSource fields to the DataPropertyName of each Column and set the other properties.
At run-time, your DataSource fields will be bound to the Columns which have a corresponding DataPropertyName value."

Why am I Getting Null DataTable?

I created a very simple test to boil this issue down to common elements. I have an MS Access DB with 1 and only 1 table in it. 2 columns (ID, Name) and added 4 rows of data to the table. Here is the steps to reproduce the problem:
I added a DataSet containing a DataTable to datasources of my project. (Add new datasource from database)
I added a test Form1 with 1 and only 1 control, a DataGridView
Using designer I bound that grid to the table in the datasources of project. (Using DataSource property of DataGridView at design-time).
My Form_Load looks like this:
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'testDBDataSet.TestTable' table. You can move, or remove it, as needed.
this.testTableTableAdapter.Fill(this.testDBDataSet.TestTable);
BindingSource bs = (BindingSource)dgvTestData.DataSource;
DataTable dt = (dgvTestData.DataSource) as DataTable;
}
When I step past the last line of code, dt is null. What am I missing?
I know it is something simple and probably obvious, but I don't see it. If I just run the code back to the form, the DataGridView has data in it.
What am I doing wrong? How can I get the underlying DataTable from BindingSource which is set as DataSource of my DataGridView using above steps?
What am I doing wrong?
When dgvTestData.DataSource is BindingSource you can not expect dgvTestData.DataSource as DataTable return anything else than null.
The as operator is like a cast operation. However, if the conversion isn't possible, as returns null instead of raising an exception.
How can I get the underlying DataTable from BindingSource?
It seems you bound the grid to a binding source which its DataSource is set to a DataSet and its DataMember is name of a DataTable. It's the default behavior when you set data source of grid to a table using designer or drag a Table from Data Sources Window and drop on your form. So if you are following default scenarios, you can use such code to get the DataTable:
BindingSource bs = (BindingSource)dgvTestData.DataSource;
var dt = (bs.DataSource as DataSet).Tables[bs.DataMember];
If you are using any other scenario different than the default scenario, you can simply use DataSource and DataMember properties of your binding sources to extract the DataTable which is using for data-binding.
Is there a better way?
It depends, why not simply use:
var dt = this.testDBDataSet.TestTable;

Why is data disappearing from cells in a column I'm adding to a DGV after I sort a column?

I have a DataGridView that I populate by binding it to a DataTable returned from an sql query, which works as you'd expect.
Later on if a user wants to see additional information in the DataGridView by clicking a button I manually add a column to the end of the DataGridView - dgvOrders.Columns.Add("profit", "Profit"); and populate it with data based on logic determined by other DataGridColumns cells values.
It all works perfectly until you click one of the column headers to sort the data, at which point the data is sorted correctly BUT every cell in my new column loses it's value and shows as empty. On top of that I don't seem to be able to filter by my new column at all either. Is it a no-no to manually add columns to a databound DataGridView or am I missing something else here?
Firstly, I would urge you to share your code, its much easier to look than guessing.. Sounds like your problem is with the data binding. Personally I don't like working directly with the data grid. Its much easier to work on your model or adapter and then rebind:
..your model container or adapter, e.g. SqlDataAdapter gets updated here...
DataTable dt = new DataTable();
adapter.Fill(dt);
dgvOrders.DataSource = dt;
dgvOrders.Columns.Add("yourcolumn", "yourcolumn");
otherwise if you prefer to work directly on the dgvOrders, do something like that:
dgvOrders.Columns.Add(new BoundField { DataField = "yourcolumn"})
dgvOrders.DataSource = yourdatasource;
dgvOrders.DataBind(); //dont forget to databind again!

C# Ordinals of Columns of Datatable changed after setting Bindingsource as datasource of Datagridview

i use a datatable as datasource of my bindingsource.
The columns have the following order and ordinals:
Before setting the datasource of datagridview
When I assign the bindingsource to the datasource property of my datagridview the
ordinals changes:
Afer settting the datasource of datagridview
The last column ("Gesamt") gets the ordinal 0.
How can I avoid this effect?
This is the relevant code:
BsData.DataSource = myPR.PR_Data; //PR_Data is the datatable ,
//BsData is the bindingsource
dgvSchoolGrade.DataSource = null; //dgvSchoolGrade is the Datagridview
//Before
dgvSchoolGrade.DataSource = BsData;
//After
Thank you very much in advance!
Sascha

Datagridview rows are 0 even though I have a datasource

I am programmatically creating a datagrid view and binding data to it. For some reason the rows are empty. Strange as it may seem but I do not want to display the Datagrid view just pass it into a class which extracts the data as Excel and CSV.
this.DataGridView = new System.Windows.Forms.DataGridView();
System.Windows.Forms.BindingSource bindingSource = new System.Windows.Forms.BindingSource();
bindingSource.DataSource = DataSource;
this.DataGridView.AutoGenerateColumns = autoGenerateColumns;
this.DataGridView.DataSource = bindingSource;
Int32 rowCount = this.DataGridView.Rows.Count; // 0!
Is there any way after creating the data source I can get the rows to populate?
Thanks
I believe DataGridViews don't bind/contain any rows until they are actually rendered.
#paul.abbott.wa.us is right.
You can move the code that count the rows in DataGridView.DataBindingComplete Event
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.databindingcomplete.aspx
I wonder why you need a DataGridView without displaying it on the UI in the first place. There are couple of alternates I can suggest
Let go the DataGridView and use DataTable/IEnumerable to keep the data. Both can provide you with
counts.
If you insist on keeping the DataGridView then something like
(Assuming its bound to a DataSet)
((DataSet)dataGridView1.DataSource).Tables[0].Rows.Count
would also work.
In order to see data grid, you should add it to parent controls collection:
DataGridView = new System.Windows.Forms.DataGridView();
Controls.Add(DataGridView);
Currently your grid do not have any parent and cannot be displayed.

Categories