Adding rows to datagridview with combox - c#

I have a DataGridView that I am programmatically adding rows to.
There are three columns: a TextBox column and two ComboBox columns. I've got the 1st and 3rd columns down pat because I know the text I am going to add. For the 3rd column (ComboBox) every row has the same 3 ComboBox item options.
It's the second column I'm having trouble with, where I need to have a different set of ComboBox items for each row. Is this possible?
I'll add my code below, let me know if anything's unclear. I'm looping through a list of folders, comparing the current name with a dataset value and needing to add the dataset name to the combobox items for the row if they "match".
//Loop through each local folder
string thisLocalFolder = Path.GetFileName(localFoldersArray[j].ToString());
//helpdeskCompanyNameMatchColumn.Items.Clear();
for (int i = 0; i < companyDataSet.Tables[0].Rows.Count; i++)
{
//Loop through each downloaded company name to compare
string dataComapanyToCompany = companyDataSet.Tables[0].Rows[i]["Company"].ToString();
if (dataComapanyToCompany.Contains(thisLocalFolder))
{
Console.WriteLine("Match: " + thisLocalFolder + " is close enough to " + dataComapanyToCompany);
//Add the possible match to the combobox item options, for this row
helpdeskCompanyNameMatchColumn.Items.Add(dataComapanyToCompany);
}
}

Related

"DataGridView.Columns[i].Name" Empty for last column

I have created a windows form application using C#.
I am trying to loop at the columns/cells and populate the column names as cell values. (Just for 1st row)
It works fine for all cells, except the last cell. That is, in the displayed data grid, all cells except the last cell has their corresponding column names.
I am unable to point out what's happening.
When I access 'DataGridView.Columns[i].HeaderText' for the last cell it returns the correct header text.
But When I access 'DataGridView.Columns[i].Name' for the last cell, it is empty.
I have also verified the bound column properties and can confirm that it matches what I have for the other columns.
Please find below my code:
for (int i=0;i< DataGridView.Columns.Count;i++)
{
fieldName = DataGridView.Columns[i].Name.Replace("_filter", "");
if (!String.IsNullOrEmpty(DataGridView.Columns[i].Name))
{
DataGridView.Rows[0].Cells[i].Value = DataGridView.Columns[i].Name + i;
}
else
{
DataGridView.Rows[0].Cells[i].Value = DataGridView.Columns[i].HeaderText;
}
}
Any help is much appreciated.
Below is screenshot of my BoundColumns screen
Bound Columns Screen:
Below is screeshot of my output, showing the last column
Final Output. The Email ID column is expected to have the column name, not the column text

Displaying original rowindex after filter in datagridview

Just want to ask on how to get the original row index of selected row in DataGridView after filter.
I have DataGridView with 2 columns :name and age. And I have a TextBox that serves as filter. Let's say I have 8 records and upon filtering it goes to only 4 records and upon clicking the last record, I get row index of 4, while I need to get the original index of this row and display it on MessageBox. How will I do it?
Thank you.
Original row index means the index of the DataRow in the DataTable which can be found by DataTable.Rows.IndexOf(row). So to find the original index of the row you can use the following code:
var r = ((DataRowView)BindingContext[dataGridView1.DataSource].Current).Row;
var index = r.Table.Rows.IndexOf(r);
In case you are interested to do that for all rows in the DataGridView, as also is mentioned by Taw in comments, you can look into the DataBoundItem of the DataGridViewRow:
var r = ((DataRowView)dgvRow.DataBoundItem).Row; // dgvRow is a row of the DataGridView
var index = r.Table.Rows.IndexOf(r);
From your comments you said that you need it to display current record selected so I will not directly answer How to get row index on filtered table but how to get current record selected.
So to simply get current selected record use this code:
//Use this one if your datagridview SelectionMode is not FullRowSelect
DataGridViewRow row = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex];
string name = row.Cells["Name"].Value.ToString();
int age = Convert.ToInt32(row.Cells["Age"].Value);
//If your datagridview SelectionMode is FullRowSelect then use this
DataGridViewRow row = dataGridView1.SelectedRows[0];
string name = row.Cells["Name"].Value.ToString();
int age = Convert.ToInt32(row.Cells["Age"].Value);
Reza answered main part of your question but if for some reason it is not working you can use this since your primary key is your NAME
foreach(DataRow r in yourDataTable.Rows)
{
if(r["NAME"].ToString() == row.Cells["Name"].Value.ToString()) //This row.cells... is the one from above code
{
int originalRowIndexInDataTable = dt.Rows.IndexOf(r);
return;
}
}

Controling ItemChecked event after filtering data in a CheckedListBox

I have a custom UserControl that has a TextBox, a Button and a CheckedListBox.
the DataSource of the CheckedListBox is a DataTable - m_CheckBoxItems.
TextBox has a TextChanged event on it.
every letter entered into the TextBox causes the DataSource to change according to what values where found.
DataRow[] filteredRows;
if (string.IsNullOrEmpty(filter))
{
DataTable tmp = m_ChecBoxItems.Copy();
((ListBox)checkedListBox).DataSource = new BindingSource(tmp, null);
}
else
{
filteredRows = m_ChecBoxItems.Select("Text like '" + filter + "%'");
DataTable tmp = filteredRows.CopyToDataTable();
((ListBox)checkedListBox).DataSource = new BindingSource(tmp, null);
}
((ListBox)checkedListBox).DisplayMember = "Text";
((ListBox)checkedListBox).ValueMember = "Id";
now, my problem is that when i click one of the options, the OnItemClicked event fires and checks the selected index according to what the old DataSource was and then after that it get into the checkedListBox_ItemCheck event where i have some code to change the checkstate of the correct index that i intentionally click, based on where it was before the DataSource changed.
for example: m_CheckBoxItems has 100 items in it. i type "d" and the datasource changes to only values that start with "d". then i click one of the items (lets say on the second row) and then inssues the chaos. the OnItemCheck event fires containing the index that was clicked according to what i see i.e: 1 and checks that index. then it goes into checkedListBox_ItemCheck event with my own code in it and checks the original index that belonged to the specific row that i chose (lets say it was index 34).
so now i have two items checked instead of just the one that i wanted (index 34)
with every item check i also put that datarow into a list of selected datarows for future handling so that part is good, i get the row that i clicked on (index 34) and not the row that was checked by mistake (index 1).
this is my question:
how can i prevent this situation?
how can i make the checkedlistbox to check only the row that i wanted and not two rows?

How to get the value of a selected item in a combobox in a datagridview

I have a combobox Supplier with a list of Suppliers. When a supplier is selected, the values in a combobox called itemName change to list all the items supplied by that supplier. The itemName combobox is in a datagridview. I have all this working to that point.
Now, what I want to do is when an item is selected in the itemName combobox, I want to update another column Unit Price in the datagridview with the unit price of that item. What I can't figure out, is how do I get the value of the selected item in the itemName combo? I know when it's in a datagridview, it's not like a normal combobox.
I am guessing this may be what you are looking for. Assuming you know the column of your comboBox, you can grab the DataGridViewComboBoxCell by using:
DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[RowIndex].Cells[ColumnIndex];
Above the row and column indexs are what you need to supply. Once you have the ComboBoxCell then you can get its value or items index with:
if (cb.Value != null)
{
// do stuff
MessageBox.Show(" Index of current comboBox items is: " + cb.Items.IndexOf(cb.Value) + " current displayed value: " + cb.Value);
}
else
{
MessageBox.Show("Combo Box is empty?");
}
Hope this helps.

fill a grid with selected rows of another grid

Code is C# and the datagrid is in XAML. The problem here is that we have one grid with rows of articles. When we select a row, this row must be immediately added to a second grid. We use for this a list, which is defined on a higher level. When we select one row, this works fine. When we select another row, this row not appears in the second grid.
However: with debugging we see that after the first selection the count of the itemsource = 1 and the count of the listgrid2 = 1.
After the second selection the count of the itemsoucre = 2 and the count of listgrid2 = 2.
But there are never filled up more than one row in the second grid..
Is there anyone familiar with this problem? Thanx in advance
int rijindex = grid1.SelectedIndex;
if (rijindex != -1)
{
int artnrGrid2 = listartb2[rijindex].artbnr;
string artnaamGrid2 = listartb2[rijindex].artbnaam;
dbGrid2 x = new dbGrid2('X', artnrGrid2, artnaamGrid2);
listgrid2.Add(x);
grid2.ItemsSource = listgrid2;

Categories

Resources