I open a CSV File in my DataGridView. When i click on button "Down" the next row is selected.
Problem: when I open a new CSV and click "Down", automatically the selection jumps to the row number of the last selected number of the old CSV.
Example: I select row 11 and open a new file. Row 1 is selected until I press "Down". Instead of row 2, row 11 is selected.
private void btn_down_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count != 0)
{
selectedRow++;
if (selectedRow > dataGridView1.RowCount - 1)
{
selectedRow = 0;
port.Write("...");
}
dataGridView1.Rows[selectedRow].Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;
}
}
You should not use an internal counter to store the selected row since the selection could be changed by other components (in your case by changing the data source). Just make use of the dataGridView1.SelectedRows to get the currently selected row. Based on this row select the next one. Here is a simple implementation:
private void btn_down_Click(object sender, EventArgs e)
{
//Make sure only one row is selected
if (dataGridView1.SelectedRows.Count == 1)
{
//Get the index of the currently selected row
int selectedIndex = dataGridView1.Rows.IndexOf(dataGridView1.SelectedRows[0]);
//Increase the index and select the next row if available
selectedIndex++;
if (selectedIndex < dataGridView1.Rows.Count)
{
dataGridView1.SelectedRows[0].Selected = false;
dataGridView1.Rows[selectedIndex].Selected = true;
}
}
}
Related
How to set focus to the absolute last row?
I tried all the solutions I found in StackOverflow, but it always goes to the Row before the last, not the last one.
when I edit a value in the Quantity Column and press enter, I add two rows: the colored one which is read-only and also another row where I can add the next item.
Now, when I press Enter after typing the quantity, the focus goes to second row quantity: I need it to go to the first Column (Barcode) of the last row.
private void onCellEditEnd(object sender, DataGridViewCellEventArgs e)
{
if(header == barcode)
{
dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value = item.Qty;
dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value = item.Item;
dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value = item.Mrp;
dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].Value = item.Discount;
dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[5].Value = item.Cgst;
dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[6].Value = item.Sgst;
dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[7].Value = item.Amount;
dataGridView1.Rows.Add();
dataGridView1.Rows[dataGridView1.CurrentRow.Index+1].Cells[1].Value = item.Details;
dataGridView1.Rows[dataGridView1.CurrentRow.Index+1].Cells[4].Value = item.Discount;
dataGridView1.Rows[dataGridView1.CurrentRow.Index+1].Cells[5].Value = item.Cgst;
dataGridView1.Rows[dataGridView1.CurrentRow.Index+1].Cells[6].Value = item.Sgst;
dataGridView1.Rows[dataGridView1.CurrentRow.Index + 1].ReadOnly = true;
dataGridView1.Rows.Add();
}
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2];
}
else if(header == quantity)
{
dataGrid.CurrentCell = dataGrid.Rows[2].Cell[0]; //tried this
dataGridView1.Rows[dataGridView1.RowCount - 1].Selected = true; //tried this
}
}
The second row is part of the first row, so on the CellEndEdit of Quantity I want the focus to go to the 3rd Row, in the Barcode Column, which I have added in the last line of code, using dataGridView1.Rows.Add();
To select a specific row you must use
dataGrid.Rows[index].Selected = true;
Wherever you make a change and you want the last row to be selected, you must use the following code :
dataGrid.Rows[dataGrid.RowCount - 1].Selected = true;
I had the same problem as you a couple weeks ago this is what I did.
private void gridControl1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
int indexOfRecent = gridView1.DataRowCount - 1;
gridView1.FocusedRowHandle = indexOfRecent;
}
}
Im working in a lab for covid. We are sending result via excel. I made a template for easy edit excel file. I searched too much but i cant find solution for this request.
in this picture after selected items and push "tamamlandı" button, i want to change dataviewgrid second column change custom data
for example: if selected 1,5,13,48 in checkedlistbox, change 1,5,13,48 rows second column data to "POZ"
You can get all checked items via CheckedItems property. Then you can use the foreach statement to traverse it.
private void btntamamlandı_Click(object sender, EventArgs e)
{
foreach(var i in checkedListBox1.CheckedItems)
{
// get the checked item value
// rowindex starts from 0, so need to subtract 1
int rowindex = Convert.ToInt32(i.ToString()) - 1;
dataGridView1.Rows[rowindex].Cells[1].Value = "POZ";
}
}
Update: delete selected rows
private void btnDelete_Click(object sender, EventArgs e)
{
// store checkedListBox1 selected index
List<int> indexs = new List<int>();
foreach (var i in checkedListBox1.CheckedItems)
{
indexs.Add(Convert.ToInt32(i.ToString()) - 1);
}
// selected row count
int count = indexs.Count;
for (int index = 0; index < count; index++)
{
dataGridView1.Rows.RemoveAt(indexs[0] - index);
indexs.RemoveAt(0); // remove the first item
}
}
I have a DataGridView with a column which is filled with buttons. Now I want that with every click on the button the selected row in the DataGridView is copied and inserted directly behind the selected row. But: I only want to duplicate the value of 2 specific columns. And between this two specific columns one should be counted with numbers from 1 starting manually typed in to 2, 3, 4 and so on with every click on the duplicate button. The other columns should be empty. How can I realize this ?
Many thanks in advance
I hope it does the job
// Enable AllowUserToAddRows
dgv.AllowUserToAddRows = true;
private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = sender as DataGridView;
// If button clicked
if (e.ColumnIndex == 0)
{
// Get values what you want to use, the Cell 2 is already increased
var value1 = dgv.Rows[e.RowIndex].Cells[1].Value;
var value2 = dgv.Rows[e.RowIndex].Cells[2].Value;
// Define the maximum id and the row index of it
int maxId = Convert.ToInt32(value2);
int latestRowIndex = e.RowIndex;
for (int i = 0; i < dgv.Rows.Count; i++)
{
// Select the highest id of selected item and get its row index
if (dgv.Rows[i].Cells[1].Value == value1)
{
maxId = Convert.ToInt32(dgv.Rows[i].Cells[2].Value);
latestRowIndex = i;
}
}
// Insert a new row behind max id one
dgv.Rows.Insert(latestRowIndex + 1);
// Set the previously stored values and increase the stored Cell 2 value
dgv.Rows[latestRowIndex + 1].Cells[1].Value = value1;
dgv.Rows[latestRowIndex + 1].Cells[2].Value = maxId + 1;
}
}
I have a data in DataGrid with n Rows, and the DataGrid is ReadOnly. Now my goal is to when I select any row or rows and then press EDIT Button then all the selected rows only becomes ReadOnly = false. so that I want to edit some data in selected row(s). After this when I press the update button then only the selected rows are updated using EntityFramework.
I done this task in WinForm DataGridView. Now I want the same thing in WPF DataGrid.
private void editCust_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
foreach (DataGridViewRow item in dataGridView1.SelectedRows)
{
for (int i = 1; i <= 3; i++)
{
item.Cells[i].ReadOnly = false;
}
}
}
else
{
MessageBox.Show("No Row Is Selected To Edit.");
}
}
private void updateCust_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
foreach (DataGridViewRow item in dataGridView1.SelectedRows)
{
cutable.Customer_Name = (string)item.Cells[1].Value;
cutable.Counter = int.Parse(Convert.ToString(item.Cells[2].Value));
cutable.Buying_Cost = float.Parse(Convert.ToString(item.Cells[3].Value));
for (int i = 1; i <= 3; i++)
{
item.Cells[i].ReadOnly = true;
}
}
db.SaveChanges();
MessageBox.Show("Record Is Update.");
}
else
{
MessageBox.Show("No Row Is Selected To Update.");
}
}
First convert DataGridView to DataTable then to DataGrid
For DataGridView to DataTable
Use this:
DataTable myDataTable=(DataTable)(myDataGridView.DataSource);
For more reference go through DataGridView to DataTable
Now for DataTable to DataGrid
Use this:
myDataGrid.ItemsSource=myDataTable.DefaultView;
myDataGrid.AutoGenerateColumns = true;
myDataGrid.CanUserAddRows = false;
For more reference go through DataTable to DataGrid
Comment for any query
UPDATE:
Try to create to your own code and logic, do not depend on direct solutions given by someone.
Well a similar issue I found in StackOverflow[Solved].
Refer to this question how to edit select row in datagrid in wpf
Hope it helps!
I have 2 datagridview in WinForms
dgv1
dgv2
dgv1 has some columns including 1 CheckBoxColumn ,dgv2 has Some columns but no CheckBoxColumn .
I want functionality which enables me , when i check CheckBoxColumn ,data of that particular Row should be selected and go into Dgv2 .
this is how i put selected rows from dgv1 to dgv2 .
But if i cancel check (or unckeck) ,checked checkBox in dgv1 that particular row should be deleted from dgv2 .
Help me to fix this . i'll be thankful of u guys ,looking forward to this. :)
here is my code
consider datagridview6= dgv1 and datagridview10 = dgv2
private void dataGridView6_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView10.Visible = true;
if (dataGridView6.CurrentCell.ColumnIndex == 7)
{
if (dataGridView6.CurrentCell.Value != null)
{
bool checkstate = (bool)dataGridView6.CurrentCell.Value;
if (checkstate == false)
{
dataGridView6.CurrentCell.Value = true;
}
else
{//here help in logic to delete unchecked row
dataGridView6.CurrentCell.Value = false;
int j= int.Parse(dataGridView6.Rows[e.RowIndex].ToString());
dataGridView10.Rows.Remove(dataGridView10.Rows[j]);
}
}
else
{dataGridView6.CurrentCell.Value = true;
dataGridView10.Rows.Insert(i); dataGridView10.Rows[i].Cells[1].Value=dataGridView6.CurrentRow.Cells[2].Value.ToString();
dataGridView10.Rows[i].Cells[2].Value = dataGridView6.CurrentRow.Cells[3].Value.ToString();
dataGridView10.Rows[i].Cells[3].Value = dataGridView6.CurrentRow.Cells[5].Value.ToString();
}
}
/* in this logic checked row is being selected in dgv2 but on unchecking it delete is not working.
both data grid are not bound with database , m fetching value using SQlCommand class .
To doing this you need to store RowIndex of dgv2 in Checked Row's Tag property.
for example when you select any row from dgv1 at this time you are adding that records in dgv2 gridview. Now, you need to store that new row index of created row in dgv1.CurrentRow.
So, when the cell get unchecked you can find the added row from the dgv2 by using that row index.
///Inserting default records
///The fifth column value is a primary key value
///Application will check that value with second grid and performs remove and add rows
///we cannot use row index to remove row. because the row index will be changed if any row removed from dgv2.
///I have placed this code in form load event.
dgv1.Rows.Add(false, "Nimesh", "Gujarat", "India", 1);
dgv1.Rows.Add(false, "Prakash", "MP", "India", 2);
dgv1.Rows.Add(false, "Rohit", "Maharashtra", "India", 3);
dgv1.Rows.Add(false, "Jasbeer", "Panjab", "India", 4);
dgv1.Rows.Add(false, "Venkteshwar", "Karnatak", "India", 5);
dgv1.Rows.Add(false, "Rony", "Delhi", "India", 6);
///We cannot use CellChange event becauase it will be executed after cell end edit.
///We cannot use CellContentClick event coz it will not call when you double click or clicking quickly multiple times.
///So we are using CellMouseUp event
private void dgv1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (dgv1.CurrentCell == null)
return;
dgv2.Visible = true;
if (dgv1.CurrentCell.ColumnIndex == 0)
{
bool bValue = (bool)dgv1.CurrentCell.GetEditedFormattedValue(e.RowIndex, DataGridViewDataErrorContexts.CurrentCellChange);
if (bValue)
{
for (int iRow = 0; iRow < dgv2.Rows.Count; iRow++)
{
if (dgv2.Rows[iRow].Tag != null && dgv2.Rows[iRow].Tag.Equals(dgv1.CurrentRow.Cells[4].Value))
return;
}
int iNewRowIndex = dgv2.Rows.Add();
dgv2.Rows[iNewRowIndex].Cells[0].Value = dgv1.CurrentRow.Cells[1].Value;
dgv2.Rows[iNewRowIndex].Cells[1].Value = dgv1.CurrentRow.Cells[2].Value;
dgv2.Rows[iNewRowIndex].Cells[2].Value = dgv1.CurrentRow.Cells[3].Value;
dgv2.Rows[iNewRowIndex].Tag = dgv1.CurrentRow.Cells[4].Value;
}
else
{
if (dgv1.CurrentRow.Cells[4].Value != null)
{
for (int iRow = 0; iRow < dgv2.Rows.Count; iRow++)
{
if (dgv2.Rows[iRow].Tag != null && dgv2.Rows[iRow].Tag.Equals(dgv1.CurrentRow.Cells[4].Value))
{
dgv2.Rows.RemoveAt(iRow);
break;
}
}
}
}
}
}
Add hidden columns on the both DataGridView.
When user check the checkboxcolumn assign some unique value to the hidden column.
Copy all the cells of the row from the first DataGridView to the second.
When user uncheck the check box, just find the row by the value of the hidden column.
RemoveAt() the row.