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
Related
I have the DataGridView below and I need to check the cell according to the receipt of some files, I am looking for the best way to do this and I thought it would be possible to insert a value in the cell by the name of the column and the name of the row
The positions are
If I wanted to mark the position 0,0 I would look for 31 - SEGUROS (Name of the column) and 01 (Name of row) and then it would be marked the cell 0,0
It is possible?
DataGridView has a CurrentCell property that includes a RowIndex and ColumnIndex.
There is also the CellEnter event where the DataGridViewCellEventArgs includes a RowIndex and ColumnIndex.
There is the Rows and Columns properties that have overloads to accept an index or a name. However, there is a difference between the name of a column and the column heading. In your case "31 - SEGUROS" is heading of the column and not its name. Each Column object has a HeaderText property.
If a column or row is not selected, its index will be -1.
Are you looking for row and column indexing? I read your question three times and still it looks like it...
VB.NET:
Dim MyVal = 1234
Dim ColIdx = 5
Dim RowIdx = 10
Me.DataGridView1.Rows(RowIdx).Cells(ColIdx).Value = MyVal
C#:
Dynamic MyVal = 1234;
Dynamic ColIdx = 5;
Dynamic RowIdx = 10;
this.DataGridView1.Rows(RowIdx).Cells(ColIdx).Value = MyVal;
You can also address the rows and cells by their names, simply putting their name into double quotes.
To get column index by name of a column (not text in the heading):
VB.NET:
Private Function GetColIdx(MyStringName As String)
Dim ColIdx As Int16
For ic = 0 To Me.DataGridView1.Columns.Count - 1
Dim dc As DataGridViewColumn = Me.DataGridView1.Rows(ic)
If dc.Name = MyStringName Then
ColIdx = ic
Exit For
End If
Next
Return ColIdx
End Function
C#:
Private Object GetColIdx(String MyStringName)
{
Int16 ColIdx = default(Int16);
For (ic = 0; ic <= this.DataGridView1.Columns.Count - 1; ic++) {
DataGridViewColumn dc = this.DataGridView1.Rows(ic);
If (dc.Name == MyStringName) {
ColIdx = ic;
break;
}
}
Return ColIdx;
}
To get column index, you then simply call (C#) the function with the name of the column:
GetColIdx("SEGUROS")
Similar function can get row by name. Though it is very uncommon to use row names, it is possible. It can also be modified to match text in the heading, but names are more safe.
I have a datagridview with 5(0-5) columns. All the rows value I retrieve from the hashtable created.
Now I've set a condition that state if column 4 contain empty value from hashtable then add new column next to column 4 which makes the new added column index at position 5 and the value of hashtable previously for column 5 change to column 7.
I do the code like this:
int number = dataGridView1.Rows.Add();
dataGridView1.Rows[number].Cells[0].Value = result; //id
dataGridView1.Rows[number].Cells[1].Value = newAddress; //ip
dataGridView1.Rows[number].Cells[2].Value = (string)((Hashtable)ht[1])["value"]; //name
dataGridView1.Rows[number].Cells[3].Value = (string)((Hashtable)ht[2])["value"]; //description
if (!ht.ContainsValue(3))
{
// Create a Save button column
DataGridViewImageButtonSaveColumn columnSave = new DataGridViewImageButtonSaveColumn();
// Set column values
columnSave.Name = "SaveButton";
columnSave.HeaderText = "";
//Add the columns to the grid
dataGridView1.Rows[number].Cells[4].ReadOnly = false;
dataGridView1.Columns[5].Add(columnSave); //im not sure about this codes
dataGridView1.Rows[number].Cells[6].Value = (string)((Hashtable)ht[4])["value"]; //count
}
else
{
dataGridView1.Rows[number].Cells[4].Value = (string)((Hashtable)ht[3])["value"]; //location
dataGridView1.Rows[number].Cells[5].Value = (string)((Hashtable)ht[4])["value"]; //count
}
However, i'm not sure if I do this right because I receive error at the line commented
dataGridView1.Columns[5].Add(columnSave); //im not sure about this codes
Seems like this code is wrong. Can anyone please advise?
Try dataGridView1.Columns.Insert(5, columnSave); instead.
MSDN reference: DataGridViewColumnCollection.Insert Method
A simple way to insert a checkbox to certain column of Data grid:
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
dataGridView1.Columns.Insert(**certain column number**, chk);
for example if you want to add checkbox to column 1 you mus type
dataGridView1.Columns.Insert(0, chk);
I've a datatable, with column A, B, C. I've set column A's "is identity" property to true, but I can't add any row to the table now.
The code I'm trying is this:
dsA.dtA row = dsA.dtA.NewdtARow();
row.B = 1;
row.C = 2;
dsA.dtA.Rows.Add(row);
I'm getting NoNullAllowedException, but I don't understand why. Column A is PK as well. If I tried to set row.A = 5 (or any similiar) I'll get an error when I try to update the datatable saying "cannot insert explicit value for identity column in table when identity_insert is set to off"
How can I solve this issue? It's frustrating.
Do this way. Reference link
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.AutoIncrement = true;
column.AutoIncrementSeed = 1;
column.AutoIncrementStep = 1;
// Add the column to a new DataTable.
DataTable table = new DataTable("table");
table.Columns.Add(column);
DataRow oRow = table.NewRow();
table.Rows.Add(oRow);
Open the designer of the dataset xsd file and set the AutoIncrement, AutoIncrementSeed and AutoIncrementStep property of the column A in datatable for an existing column.
Try one of the these two:
Set field values:
row.A = null;
row.B = 1;
row.C = 3;
Add row to DataTable:
dtA.Rows.Add(null,1,2);
They are both the same just try any of them and it should get you going. Also remember that whenever you want to make a column auto-increment in DataTable then you have to insert null into it.
The way that I do it is
public void AppendtodtA(int num1, doubledouble1, string string1)
{
object[] RowArray = new object[] {null,(int)num1, (double) doubledouble1, (string) string1}
DataRow CurrentRow = dtA.NewRow();
CurrentRow.ItemArray = RowArray;
dtA.Rows.Add(CurrentRow);
}
use ItemArray property and leave a null in the place of the autoincremented column
AppendtodtA(MyNumber,MyDouble,MyString);
Then just call the method with your variables.
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;
}
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
}
}