Selection Start Row in DatagridView - c#

I am using the windows DataGridView, in this grid i allowed the multiple rows selection.
When i am checking the dataGridView1.SelectedRows[0].Index it's giving last selected rows Index. Just i want from which row the selection started(Start Row).

You can try with this code
Int32 selectedRowCount =
dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected);
dataGridView1.SelectedRows[selectedRowCount - 1 ].Index.ToString();

you can use foreach loop to check the lowest indexed Row.
Because Rows in SelectedRows collection are added in order as you select the row.
Here is complete foreach loop..
int i=int.MaxValue;
foreach (DataGridViewRow rw in dataGridView1.SelectedRows)
{
if (rw.Index < i)
{
i = rw.Index;
}
}
DataGridViewRow r = dataGridView1.Rows[i];
it will return you lowest indexed selected row.

You can try with something like
dataGridView1.SelectedRows[dataGridView1.SelectedRows.Count - 1].Index
But from my point of view, the selection order in the SelectedRows is not something that should be relied on anyway. As far as I know, the documentation doesn't specify any particular order... You should do something by yourself if that's important for your application.

Related

How can I get all the values of the 1st column of every single row in a dataGridView?

I have a DataGridView that looks like the following.
I am required to get ID value of every row and perform an operation with it in every loop.
I am able to get the ID value if I select a particular row. However what I am trying to do is basically select all the rows pro-grammatically and get the ID value in every count of my for loop.
Ideally I'd get 2645 in my first iteration, 1723 in the second iteration and so on.
What I tried so far is:
if (dataGridView1.SelectedCells.Count > 0)
{
int selectedrowindex = dataGridView1.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = dataGridView1.Rows[selectedrowindex];
string IDStr = Convert.ToString(selectedRow.Cells["ID"].Value);
//an operation with the ID Value
}
This only worked for a single row and when I selected the row manually. For every row in the DataGridView, I tried to add dataGridView1.SelectAll() and looped inside foreach(DataGridViewRow row in dataGridView1.SelectedCells) and performed all the steps above but that did not work either.
What am I doing wrong here? Any idea/help would be greatly appreciated. I am happy to clarify if any info is unclear in the question.
have not tested this code but something like this should work
if (dataGridView1.SelectedCells.Count > 0)
{
foreach (GridViewRow row in dataGridView1.Rows)
{
int id = row[0].text;
}
}
put a breakpoint and see if it gets the id,
hope this helps

Programmatically updating selected rows misses the last one in dgv.DataSource.GetChanges()?

I have a data grid view with a column (combobox column). The following function has been implemented.
Select several rows (click the left most row header and drag).
Programmingly set the value to something (see following). All the selected rows changes.
foreach (DataGridViewRow item in dgv.SelectedRows)
{
item.Cells["cbxxxxx"].Value = p;
}
dgv.EndEdit();
Click the save button to save the changes. However, the last row is excluded in the (dgv.DataSource as DataTable).GetChanges(). The count of the changed data table is always one less than the selected rows. The missing row is the last one (with the black triangle).
How to fix the problem?
The last row presumably is still the active row, so you need to end the edit through the BindingContext:
foreach (DataGridViewRow item in dgv.SelectedRows) {
item.Cells["cbxxxxx"].Value = p;
}
this.BindingContext[dgv.DataSource].EndCurrentEdit();

winform multiple select grid row and get row index of selected rows

I have a databound gridview in my winform. I want to know how to get the index of Currently selected rows i.e multiple rows.
I am able to do this with a single row. but is there a way I can have a checkbox or something in which I can index of multiple rows.
The Image below will help u understand better of my requirement.
First set CellContentClick event to your DataGridView.
dataGridView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.onCellContentClick);
For every cell click it will invoke the following method. Here you can create a list and populate it with clicked row index.
public void onCellContentClick(DataGridViewCellEventArgs cell)
{
// Check whether selected cell is check box column, here 0 indicates the check box column.
if (cell.ColumnIndex == 0)
{
bool isChecked = (Boolean) dataGridView[cell.ColumnIndex, cell.RowIndex].EditedFormattedValue;
if(isChecked)
{
// Below will give you the selected cell row index, for multiple rows you can populate those index in list or whatever you convenient with.
cell.RowIndex;
}
}
}
Use DataGridView.SelectedRows property to get all selected rows.
To select multiple rows:
DataGridView.MultiSelect = true;

Get row index by column value in DataGridView

How can i get the index of a datagridview row by column value of that row ? And also , how can i update the datagridview when a value of a column has been changed ?
Re comment -- find which rows contain a value of x:
Should be something like this (not tested)
List<int> rowsOfInterest = new List<int>();
foreach(DataGridViewRow row in dgv.Rows)
if (row.Cells["Column I want"].Value == ValueIWant)
rowsOfInterest.Add(row.Index);
I think what you want is this, but your question is not 100% clear.
foreach(object value in dgv.Rows[rowindex].Cells)
// check is value is one needed

Delete a Row from a DataGridView given its index

My DataGridView is a single line selection and theres a rowEnter Event where I get the line index every time the selected line changes.
private void rowEnter(object sender, DataGridViewCellEventArgs e)
{
currentRowIndex = e.RowIndex;
}
when I press a delete button I use the same index to delete the row
myDataSet.Avaliado.Rows[currentRowIndex].Delete();
avaliadoTableAdapter.Update(myDataSet.Avaliado);
it works fine if no column in the DataGridView is sorted, otherwise a get an error. What should be the way to know the row index in the dataset that corresponds to the rowindex from the DataGridView?
You don't need to be grabbing the current row index every time a new row is selected. Try something like this instead:
if (_Grid.SelectedRows.Count <= 0) { return; } // nothing selected
DataGridViewRow dgvRow = _Grid.SelectedRows[0];
// assuming you have a DataTable bound to the DataGridView:
DataRowView row = (DataRowView)dgvRow.DataBoundItem;
// now go about deleting the row however you would do that.
If you've got some other sort of data type bound to each row of the grid, simply cast the DataGridViewRow.DataBoundItem to whatever your data type is.
You can find the currently selected row at the time that you are doing the delete:
if(myDataGridView.SelectedRows.Count > 0)
currentRowIndex = myDataGridView.SelectedRows[0].Index;
I usually have the Primary Key for that row as a hidden column (my convention is using the first using column).
I can then ask my persistence Layer to do the rest.

Categories