Reputation: 55
I want to add an extra cell to each column created where its value is from the cells in each row. I have this code but doesn't work:
i = 0;
Double X1 = Convert.ToDouble(DGV_Points.Rows[i].Cells[6].Value);
Double X2 = Convert.ToDouble(DGV_Points.Rows[i++].Cells[6].Value);
Double LapLength = X1 - X1;
this.DGV_Points.Rows.Add();
this.DGV_Points.Rows[0].Cells[1].Value = LapLength;
this.DGV_Points.Rows[1].Cells[1].Value = LapLength;
I have also tried this exaample:
foreach (var item in _model.SelectedItems)
{
var row = new DataGridViewRow();
var codeCell = new DataGridViewComboBoxCell();
codeCell.Items.AddRange(ItemCode.Items);
codeCell.Value = ItemCode.Items.GetListItem(item.Code);
var nameCell = new DataGridViewComboBoxCell();
nameCell.Items.AddRange(ItemName.Items);
nameCell.Value = ItemName.Items.GetListItem(item.Name);
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Id });
row.Cells.Add(codeCell);
row.Cells.Add(nameCell);
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Units });
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Quantity });
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.PriceLt });
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.PriceEu });
itemView.Rows.Add(row);
}
But it also doesn't work.
Does anyone have any suggestions?
Upvotes: 0
Views: 8309
Reputation: 54433
Two ways:
Column
first, then you can replace any Cell
in the Column
by whatever type of Cell
you want to create:DataGridViewTextBoxColumn normalColumn = new DataGridViewTextBoxColumn();
DGV_Points.Columns.Insert(yourColumnIndex, normalColumn);
DGV_Points.Rows.Add(11); // we need at least one row where we can insert a cell
DataGridViewComboBoxCell aCell = new DataGridViewComboBoxCell();
DGV_Points.Rows[someRowIndex].Cells[yourColumnIndex] = aCell;
Column
to have the ColumnType
you want and all Cells
in the Column
will have the right Type
from the beginning: DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn();
comboCol.HeaderText = "yourHeaderText";
DGV_Points.Columns.Insert(yourColumnIndex, comboCol);
Note that the first method creates only as many DataGridViewComboBoxCells
as you make it to whereas the second way immediatly create a DataGridViewComboBoxCell
in each Row
. It is up to you to decide what you want..
Instead or Inserting
at a given position you may want to simply Add
the Column
. The Add
method returns the Index
of the new Column
.
Since we are adding a DataGridViewComboBoxCells
here is an example how you can fill the Items
of the new dropdown cell:
List<string> items1 = new List<string> (){ "111", "222", "333", "444", "555" };
((DataGridViewComboBoxCell)DGV_Points.Rows[2].Cells[0] ).Items
.AddRange(items1.ToArray());
DGV_Points.Rows[2].Cells[0].Value = "222";
This fills the dropdownlist of the cell in row 2, column 0 with five string values and select the value to be "222". Note that this way you must fill the Items
of each Cell
and you can fill each one with a different list of values to choose from.
You can also set an individual Cell's DataSource
: aCell.DataSource = items1;
or you can set it for the Column
: comboCol.DataSource = items1;
Upvotes: 1
Reputation: 965
You can try like this:
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells[0].Value = "XYZ";
row.Cells[1].Value = 50.2;
yourDataGridView.Rows.Add(row);
OR
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells["Column2"].Value = "XYZ";
row.Cells["Column6"].Value = 50.2;
yourDataGridView.Rows.Add(row);
Upvotes: 0