Hello people :)
I want to bind my comboboxes to dgv cells . when dgv cells will be resize change size automaticli comboboxes size . and want to dock on above dgv cells
how can i make this?
Actually your problem is not cleared much.
Here I am adding another answer.
As you have said in one of your reply that you want to resize "yourComboboxColumn" on changing the size of "Products" Column
Put this Code: In the ColumnWidthChange event
if (DGV.Columns.Contains("yourColumn") && e.Column == dataGridView1.Columns["Products"])
{
DGV.Columns["yourColumn"].Width = e.Column.Width;
}
Edited:
To bind data of your combobox to ComboboxColumn do this
((DataGridViewComboBoxColumn) DGV.Columns["yourColumn"]).DataSource = cb.Items;
//"yourColumn" is the comboBoxColumn in DGV
// cb is the ComboBox which contains Items
Add Column of a type DataGridViewComboBox to DataGridView and bind it with your DataSource
To resize your Columns on Changing the size of DGV set AutoSizeColumnMode to Fill
DGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
Edited:
In the ColumnWidthChange event of DGV put below code
foreach (DataGridViewColumn column in DGV.Columns) //DGV is your dataGridView
{
column.Width = e.Column.Width;
}
Related
I have a class derived from DataSet that I'm using as a datasource. My DataGridView has 8 columns of which 3 are multiple value choices that are edited with drop down combo boxes.
If I let the Datasource generate the columns for the DataGridView, then all of the column types are DataGridViewTextBoxColumn. However the DataGridView is populated with my original row values.
If I set AutoGenerateColumns to false and add my own column types to the DataGridView I can add DataGridViewComboBoxColumn for the 3 columns that require it. However, the DataSource, while it contains the row data, does not populate the rows of my DataGridView.
Obviously I want both things to happen. The rows need to be populated from the DataSource and the column need to be correctly editable from the DataGridView as DataGridViewComboBoxColumns. Why can't I have both? What am I doing wrong.
Please note this is not a question about using a DataSource to populate the items in a ComboBox.
code fragment
alarmDataSet = new AlarmDataSet(sAlarms);
gridView.AutoGenerateColumns = false;
gridView.DataSource = alarmDataSet;
gridView.DataMember = sAlarms;
another code fragment
cm = new DataGridViewComboBoxColumn();
cm.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
cm.HeaderText = ci.Name;
cm.Name = ci.DataName;
cm.MaxDropDownItems = 3;
cm.Items.Add("Once");
cm.Items.Add("Repeat");
cm.Items.Add("Off");
gridView.Columns.Add(cm);
Thanks
if you set
DataGridView.AutoGenerateColumns = false
Then You need to tell DataGeridView about which column of DataGridView bind to which column of DataSource (Property Name in case of list)
DataGridView.Column[0].DataPropertyName = "Column Name In Your DataSource That is bound to DataGridView Column 0"
I have a DataGridView in my WinForms application. Basically, I want to input some 2D data there or something like that. It means, that I want to have both columns named and rows named. And so I did. But then there was a problem. When it's fine with column names, the row names tend to be not visible. For example:
The code I use to somehow "beautify" the DataGridView:
private void BeautifyTable(TableView tableView)
{
foreach (DataGridViewRow row in tableView.Rows)
{
row.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
foreach (DataGridViewColumn col in tableView.Columns)
{
col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
}
TableView is a class created by me, and it inherits from DataGridView.
So now my question would be: Is there a way to somehow make those row names/titles appear in a normal way (in this particular case those are: s0, s1, s2.., but they're like cut from the left side).
P.S Is there a good way to "stretch" the columns? I mean if I have f.e 10 columns they would fill the whole DataGridView width, but if I would add (I do this dynamically) 5, so there would be 15 columns, still they would fit, just the width of every column would decrease?
Increase the width of the row header:
dataGridView1.RowHeadersWidth =
dataGridView1.RowHeadersWidth * 2; // or another value
Try to hide those arrows by setting the RowHeadersVisible to False in the properties of the datagridview at the Form.cs [Design] or just simply add the code below.
Me.DataGridView1.RowHeadersVisible = False
I actually found the best answer by myself.
You can set the WitdthSizeMode also for row headers, like this:
tableView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
And that's the result:
I want to change a DataGridView cell value, and I can do it but cell value doesn't shown in DataGridView.
Here is the code;
Products.DataSource = productItems.DefaultView; // In here I assign the dataTable to the dataGridView.
foreach (DataGridViewRow row in Products.Rows)
{
if (row.Cells["ProductCode"].Value != null)
{
row.Cells["ProductName"].Value = sqlTrans.GetProductNameWithProductCode(row.Cells["ProductCode"].Value.ToString());
}
}
When I debug, I can see the value is assigned correctly but I can't see the value in dataGridView.
I already tried RefreshEdit, EndEdit, InvalidateCell and Refresh.
Thanks in advance.
EDIT:
After struggle few more hours, I solve this problem like this;
- After filling the DataTable with SqlDataAdapter, I added a new column named "ProductName" and then fill this column.
- And then I bound this DataTable into the DataGridView.
I might be doing something wrong but...
I have a datagridview. Its DataSource is BindingSource which has a table as a DataSource.
I created a combobox in the datagridview and filled it using:
col.Name = "employee_comb";
col.DisplayMember = "work";
col.ValueMember = "work_id";
col.DataPropertyName = "work";
col.DataSource = bs;
dataGridView1.Columns.Insert(3, col);
work contains string and work_id contains int.
I also have CurrentCellDirtyStateChanged event to check for changes.
The problem is: when I select another item in the combobox list, the datagridview automatically changes its CurrentRow to the CurrentRow with an "id" (first column) equal to ValueMember of combobox item. How do I stop it from switching?
It seems the problem occurred because datagridview and datagridviewcombobox had the same DataSource. I created new BindingSource and it works like charm!
I am using a datagridview for which I am not using any datasource. I want to dynamically allocate values to it. Create my own selected number of columns and rows and name them.
Plus I want to add images to cells instead of data .
As for changing columns text we can use
grid.Columns[0].HeaderText = "First Column";
How to change use it for labeling rows?
Set the HeaderCell.Value for the row:
DataGridView dgv = new DataGridView();
dgv.Columns.Add("Foo", "Foo Text");
dgv.Rows.Add();
dgv.Rows[0].HeaderCell.Value = "Row Text";
Form form = new Form();
form.Controls.Add(dgv);
Application.Run(form);