Why do I get the exception that Column Name is not found for MyEntity as well as FullName Columns? Although I see the column names being displayed in UI.
InitializeComponent();
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
int rowIndex = dataGridView1.Rows.IndexOf(row);
myObject = dataGridView1.Rows[rowIndex].Cells["MyEntity"].Value as IEntityObject;
fileName = dataGridView1.Rows[rowIndex].Cells["FullName"].Value.ToString();
}
Because, infuriatingly enough, that datagridview column is not actually named the same as your DataTable column name. If you look at the column collection in the designer Properties window, you will see that is probably named something like "DataGridViewColumn4" or similar.
If you know the index, you should use that, or rename the columns to the DT column names.
Incase it is not completely obvious, programmatically adding a checkbox to your datagridview with the name set should look like below:
DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
col.Width = 90;
col.HeaderText = "Sync To Vend?"; //Header cell text
col.Name = "SyncVendBox"; //This is the important part
ProductGrid.Columns.Insert(2, col);
And if you want the checkbox ticked:
for (int i = 0; i < ProductGrid.Rows.Count; i++)
{
ProductGrid.Rows[i].Cells["SyncVendBox"].Value = true;
}
Related
Hellou to everyone.
I would love the assistance from you guys.
My problem is like this:
I have this datagridview which I fill using datasource (a query to a db table). I only created manually one column which contain a checkbox. When the binding is complete I can click the checkboxes to select the row and I can select multiples rows this way.
I want to put the selected rows into a datatable (so I can read it and do some sql queries with the information on it) BUT when I read the selected rows to put them inside a datatable I need to skip the checkbox column (the first one).
How can I achieve this?
Okay, for future reference, I'm gonna post here how I fixed it.
DataTable dt = new DataTable();
if (dgvPedidos.SelectedRows.Count > 0)
{
foreach (DataGridViewColumn column in dgvPedidos.Columns)
dt.Columns.Add();
int i = 0;
foreach(DataGridViewRow row in dgvPedidos.Rows)
{
if (Convert.ToBoolean(dgvPedidos.Rows[i].Cells["chkPedido"].Value) == true)
{
dt.Rows.Add();
dt.Rows[i][1] = row.Cells[1].Value.ToString();
dt.Rows[i][2] = row.Cells[2].Value.ToString();
dt.Rows[i][3] = row.Cells[3].Value.ToString();
dt.Rows[i][4] = row.Cells[4].Value.ToString();
dt.Rows[i][5] = row.Cells[5].Value.ToString();
dt.Rows[i][6] = row.Cells[6].Value.ToString();
dt.Rows[i][7] = row.Cells[7].Value.ToString();
dt.Rows[i][8] = row.Cells[8].Value.ToString();
dt.Rows[i][9] = row.Cells[9].Value.ToString();
i++;
}
}
dt.Columns.RemoveAt(0);
I have datagridview in a tabpage. the datagridview has 6 columns. The 6th column is a comboboxcolumn. I am trying to bind a datasource to the comboboxcells. Each row would have different datasource based on the row number. The datagridview will have 10 rows always. The problem is the comboboxes are not populating any values. It just gives me blank item. Both datagrid and comboboxcolumn datasources showing data table values when if I keep break points.Could someone tell me what I am missing here?
private void BuildFreshAccessMatrix()
{
dataGridView1.AutoGenerateColumns = false;
DataGridViewComboBoxColumn cboPermissionCol = (DataGridViewComboBoxColumn)dataGridView1.Columns[5];
//cboPermissionCol.DataPropertyName = "UserLevelCategoryName";
dataGridView1.DataSource = dataProvider.GetBlankMatrixData();
int i = 1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewComboBoxCell cboPermission = (DataGridViewComboBoxCell)(row.Cells["UserLevelCategoryNameAdd"]);
cboPermission.DataSource = dataProvider.GetPermissionComboData(i);
cboPermission.DisplayMember = "UserLevelCategoryName";
cboPermission.ValueMember = "UserLevelCategoryName";
i += 1;
}
}
Try to change your code to :
DataGridViewComboBoxCell cboPermission = (DataGridViewComboBoxCell)
dataGridView1.Rows[i].Cells["UserLevelCategoryNameAdd"];
instead of :
DataGridViewComboBoxCell cboPermission = (DataGridViewComboBoxCell)
(row.Cells["UserLevelCategoryNameAdd"]);
It's a simple question but after searching a lot I didn't find a solution for it. I want to get ColumnName of a DataGridView by column tag. I had put tags on each column with column name.
EDIT:
Also I store a decimal value in tag.
This is my code:
DataGridViewColumn dgCol = new DataGridViewColumn();
dgCol.Name = "MyCol";
dgCol.Tag = 10;
DataGridView1.Columns.Add(dgCol );
While tag is of type Object. Now I want to set value of cell but I had column tag not the name neither I know column index.
DataGridView1.Rows[1].Cells[" _**Dont Have ColumnName**_ "].Value = 100;
But I had column tag. So is there any way from which I got column name from column tag?
You can iterate through all columns:
DataGridViewColumn getcol;
foreach(DataGridViewColumn c in DataGridView1.Columns)
{
if(c.Tag as String == "10")
{
getcol = c;
}
}
Now you can use this getcol.
DataGridView1.Rows[1].Cells[getcol.Name].Value = 100;
this is an improvement to my previous answer
You can use linq also and it is shorter and neat.
var col = dataGridView1.Columns.Cast<DataGridViewColumn>().Where(c => c.Tag == "10").Single();
dataGridView1.Rows[1].Cells[col.Name].Value = "100";
Try this out:
you can get columns name by following ways.
Suppose i have two columns: col1 and col2
1. Directly using the column object
dataGridView1.Rows[0].Cells[col1.Name].Value = "100";
2. or by directly columns index
dataGridView1.Rows[0].Cells[dataGridView1.Columns[0].Name].Value = "100";
Hope this helps
Is it possible in a single hit to remove all columns from a datagrid?
I know I could loop though and remove them one by one, or remove the whole thing and create it.
But it is possible to simply clear columns and leave the grid in place so users can start fresh.
i.e. put it back to its original state.
OK here is the code I have
private void PopulateGrid()
{
UserVGrid.AutoGenerateColumns = false;
UserVGrid.Columns.Clear();
List<string> _values = populate.GetVaribles;
foreach (var line in _values)
{
if (!line.Contains("Startind"))
{
string[] newline = line.Split('=');
DataGridViewColumn newColumn = new DataGridViewTextBoxColumn();
newColumn.Name = newline[0];
newColumn.HeaderText = newline[1];
newColumn.ToolTipText = newline[2];
UserVGrid.Columns.Add(newColumn);
}
}
so lets assume _values has apple, pear as values
running this method once and i get a datagrid with two colums named apple and pear.
running it a second time this time with _values containg char , table.
And i end up with 4 colums apple, pear, chair and table. What I want is the second time it is run it clears the grid fist and then applied the new colums.
Cheers
Aaron
The DataGridView columns collection has a Clear() method.
dataGridView1.Columns.Clear();
This doesn't work with a bound datagridview and autogenerated columns - in that case simply replace the datasource with null or with an empty datasource:
dataGridView1.DataSource = null;
or turn off autogeneration and create the columns in code:
dataGridView1.AutoGenerateColumns = false;
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
dataGridView1.Columns.Add(col);
These manually generated columns will be removed by the Clear() method.
Here's what worked for me
List<string> lstOverride = new List<string>();
lstOverride.Add("IssuerOnly");
lstOverride.Add("TickerOnly");
lstOverride.Add("All");
lstOverride.Add("Private Equity");
lstOverride.Add("Ignore");
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.HeaderText = "Override";
cmb.Name = "cmb";
cmb.DataSource = lstOverride;
cmb.DataPropertyName = "Override";
dgvWebData.Columns.Add(cmb);
I added a DataGridViewComboBoxColumn and had set the DataPropertyName of it to the column I needed to have the drop down.
Clear the datatable
If ds.Tables.Count > 0 Then
ds.Tables(0).Columns.Clear()
ds.Tables(0).Rows.Clear()
End If
and set the datagridview datasource to nothing
dg.DataSource = Nothing
for removing a columns of dataGridView you can do like that "P_Comment" is Columns Name
dataGridView1.Columns.Remove("p_Comment");
and for remove all columns you can use
dataGridView1.Columns.Clear();
This allowed me to delete all the data and the column headers from a datagridview
ds2.Tables.Clear();
dataGridView1.DataSource = null;
dataGridView1.Columns.Clear();
I have an application with a DataGridView. One of the columns is of the type Combobox. I want to add the items for this combobox programmatically. Here is the code I use for that:
this.dsStatussen = this.statussenMan.getAllStatussen();
DataGridViewComboBoxColumn cd = (DataGridViewComboBoxColumn)this.dgvEenheden.Columns[3];
cd.DataSource = dsStatussen;
cd.DisplayMember = "statussen";
cd.DataPropertyName = "sid";
cd.ValueMember = "status";
Then when I try to add a row I get the following error: "There is no field with the name status". I transelated the error to English because I have a Dutch error.
Here is the code I use for adding the rows:
Eenheden eenhedenMan = new Eenheden(objEvenement.eid);
DataSet EenhedenData = eenhedenMan.getAllEenheden();
foreach (DataRow dr in EenhedenData.Tables[0].Rows)
{
dgvEenheden.Rows.Add(
dr[0].ToString(),
dr[1].ToString(),
dr[2].ToString(),
Convert.ToInt32(dr[6]),
dr[3].ToString(),
dr[4].ToString(),
dr[5].ToString()
);
}
Could someone help me to figure out what I'm doeing wrong? I can't find it. This is the first time I use an DataGridView with comboboxes.
in my experience I found everything seemed to work better if you tied it in through a binding scource, then set the
bindingScource.dataScource.Rows.Add(
dr[0].ToString(),
dr[1].ToString(),
dr[2].ToString(),
Convert.ToInt32(dr[6]),
dr[3].ToString(),
dr[4].ToString(),
dr[5].ToString()
);
Select the correct row? you mean select from the dropdown to see the row inside the datagrid?
int index = dropdown.SelectedIndex();
for(int count = 0; count < dgvEenheden.Rows.Count; count ++)
{
if (dgvEenheden.Rows[count].Cells["<enter col name here>"].Value.ToString().equals(dropdown.Items[index].Text))
{
dgvEenheden.Rows[count].Selected = true; //to select the Row
dgvEenheden.Rows[count].Cells[<Cell Number>].Selected = true; //to select the specific Cell
}
}