remove default row selection datagridview - c#

I have a datagridview that when it loaded first row is selected. I want to delete a record but I don't want first row select. I want message to user that first select a row then click delete button. I try these code but they don't worked.
//kharidDataGridView.Rows[0].Selected = false;
//kharidDataGridView.ClearSelection();
//kharidDataGridView.CurrentCell = null;
I use below codes in WPF:
object item = datagrideview1.SelectedItem;
if (item == null)
{
//message to user
}
thank you

You can remove default selected row by handling data-grid loaded event
private void datagrideview1_Loaded(object sender, RoutedEventArgs e)
{
datagrideview1.SelectedIndex = -1;
}

Related

Prevent first cell of DataGridView from being selected when row header clicked

I need to allow the user to select a row by clicking on the row header to allow row deletion, but when the row header is clicked, the first cell gets focus and the text is highlighted, which I need to prevent so the user can't make accidental edits to that first cell.
Here's an example of what's happening:
As you can see, the first cell has focus and the user can edit that cell, when all that was done was the row header was clicked.
I tried the answer to this SO question, but neither event fired when I clicked a row header. Here's the code that I used for that attempt:
private void dgvCategories_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
if (e.StateChanged == DataGridViewElementStates.Selected)
{
Console.WriteLine("true");
dgvCategories.ReadOnly = true;
}
}
private void dgvCategories_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
if (e.StateChanged == DataGridViewElementStates.Selected)
{
Console.WriteLine("false");
dgvCategories.ReadOnly = false;
}
}
I also tried experimenting with the various DataGridViewEditMode options, but none of those accomplished what I need.
The DGV SelectionMode is set to RowHeaderSelect.
I still need to be able to grab the hidden "id" field in the row for the delete process. Here's my code for that:
private void btnDeleteRow_Click(object sender, EventArgs e)
{
int index = dgvCategories.SelectedRows[0].Index;
string rowId = dgvCategories[0, index].Value.ToString();
string deleteString = string.Format("DELETE FROM masterfiles.xref WHERE id = " + rowId);
conn.Open();
NpgsqlCommand deleteCmd = new NpgsqlCommand(deleteString, conn);
deleteCmd.ExecuteNonQuery();
conn.Close();
}
That works; I just need to prevent that first cell from automatically getting focus for editing when the row is selected. I still need to allow the user to select individual cells for editing, as well.
Try :
1- make the DataGridView selection by one row instead of one cell .
2- Disable allowing user to edit the DataGridView.
3- Make DataGridView ReadOnly .
Do 1 & 2 all the time except at editing operation .

How to get current row of datagridview values when clicking on button?

I have a View Model in which my data is stored to be used on the presenter and window. However, I now need to get (when pressing a button) the current row's results on the grid into my view model. I first used the mouse double click event, but the requirements changed from that to a button on the form:
private void dgvResults_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (this.colOrderNo.Index != e.ColumnIndex || e.RowIndex < 0) return;
var auditOrder = (PurchaseOrdersTrackingViewModel) this.dgvResults.Rows[e.RowIndex].DataBoundItem;
this.AuditOrderKey = $"{auditOrder.OrderNo}|{auditOrder.ItemNo}|{auditOrder.WarehouseId}|{auditOrder.ItemSequence}";
}
That's my old code. Now I need that here:
private void btnAuditLog_Click(object sender, EventArgs e)
Not sure how to refer to the RowIndex without the DataGridViewCellMouseEventArges event. I tried the CurrentRow property, but it doesn't do the job.
Just another question on the fly, if someone could assist with that as well, how do I get a value from one form to another, spesifically a properties value. It's not on the grid, in a control. It's basically just public string order that's all. I set the value on one form, then need it for query filtering on another. Let me know if you need anything else.
You want to use the SelectedRows property of the DataGridView. It tells which row is selected. In this case, you seem to want the first selected row (since you have a single property called this.AuditOrderKey).
The column order no to longer matters in this context.
if (dgvResults.SelectedRows.Count == 0) return;
var auditOrder = (PurchaseOrdersTrackingViewModel) dgvResults.SelectedRows[0].DataBoundItem;
this.AuditOrderKey = $"{auditOrder.OrderNo}|{auditOrder.ItemNo}|{auditOrder.WarehouseId}|{auditOrder.ItemSequence}";
If you don't have selected rows, CurrentRow is an option:
if (dgvResults.CurrentRow == null) return;
var auditOrder = (PurchaseOrdersTrackingViewModel) dgvResults.CurrentRow.DataBoundItem;
this.AuditOrderKey = $"{auditOrder.OrderNo}|{auditOrder.ItemNo}|{auditOrder.WarehouseId}|{auditOrder.ItemSequence}";
Use the SelectedRows property of the DataGridView. Assuming that you set your DataGridView as MultiSelect = false; and SelectionMode = FullRowSelect;
if (dgvResults.SelectedRows.Count > 0)
{
dgvResults.SelectedRows[0].Cells["yourColumnName"].Value.ToString();
}
In your button click event, it will look like this. (Assuming that your button name is btnEdit)
private void btnEdit_Click(object sender, EventArgs e)
{
if (dgvResults.SelectedRows.Count > 0)
{
string selectedValue = dgvResults.SelectedRows[0].Cells["yourColumnName"].Value.ToString();
}
}

Add Combobox to column that is already inclued in a bounded datagridview

I want to have a data gridview that is bounded to SQL data source that has a column with name status also I need this column to be combobox, to be better to the user to select the value instead of writing it, is it possible ?
I've searched and I've found this Adding bound combobox to datagridview and it answered how to add comobobox to bounded datagridview, but this is not what I need
I've also tried to make another solution in a different way by using menustrip and it works fine but I can't edit the cell value after the user select menusttrip item as the datagridview is bounded to data source
Here is a part of my code related to the 2nd solution:
dtSamples = cMaster.Select_Samples(nKeyNo, DateTime.Today, strMachine);
dataGridView_Samples.DataSource = dtSamples;
//here is the event that shows the menustrip when the user clicked on the desired column
private void dataGridView_Samples_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DataGridView.HitTestInfo hit = dataGridView_Samples.HitTest(e.X, e.Y); //Get the clicked cell
if (e.RowIndex < nNewRowIndex) //If it's a header, ignore
return;
dataGridView_Samples.CurrentCell = dataGridView_Samples[e.ColumnIndex, e.RowIndex]; //Select the cell for future info
StatusRowIndex = e.RowIndex;
if (dataGridView_Samples.CurrentCell.ColumnIndex == 4) //If this is the priority column
{
contextMenuStrip_Status.Show(Cursor.Position.X, Cursor.Position.Y); //Show the strip
}
}
}
//here is the event of selecting item of menustrip and it doesn't show the value in the cell
private void toolStripMenuItem_Repair_Click(object sender, EventArgs e)
{
if (nNewRowIndex == -1)
{
dataGridView_Samples.CurrentCell.Value = "Repair";
dataGridView_Samples.Rows[StatusRowIndex].Cells[4].Value = 4;
dataGridView_Samples.NotifyCurrentCellDirty(true);
}
else
{
dataGridView_Samples.Rows[nNewRowIndex].Cells[4].Value = "Repair";
// dataGridView_Samples.Rows[nNewRowIndex].Cells[4].Value = 4;
dataGridView_Samples.NotifyCurrentCellDirty(true);
dataGridView_Samples.BeginEdit(true);
dataGridView_Samples.EndEdit();
}
}

how to remove or disable the extra column and row selection function?

I have grid view, and I have already disable the column auto width so I can manually set the size of column. after manually resize there's a extra blank column. and what I want is :
To disable the select row function when i click on outside of the column in Active, code and generate
Remove the extra column.
I already success fully hide the remaining column with this event or code
private void gridView1_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column == null)
{
e.Handled = true;
}
}
The thing is I still can click on the outside of the genre, and the row selection still follow where I click
Handle the GridView.MouseDown event in the following way:
private void gridView1_MouseDown(object sender, MouseEventArgs e) {
GridView view = (GridView)sender;
var hi = view.CalcHitInfo(e.Location);
Console.WriteLine(hi.HitTest);
if (hi.InRow && !hi.InRowCell)
DXMouseEventArgs.GetMouseArgs(e).Handled = true;
}

How to transfer data from gridview to textbox

Suppose if I say I have selected a row in gridview and want that row to come up in to their respective textboxes. For example, I have selected a row which has First Name and Last Names and on selection of row, the data from gridview should come in to textbox on winform. Please tell me.
I am guess it is something based on selection changed event if I am right ? Like below:
private void dgv_SelectionChanged(object sender, EventArgs e)
{
string str = txtFirstName.Text;
DataGridViewRow selectedRow;
}
If I got your question right, your solution is to use DataGridView.SelectedRows Property along with DataGridView.SelectionChanged Event
DataGridView.SelectedRows Gets the collection of rows selected by the
user.
DataGridView.SelectionChanged occurs whenever cells are selected or
the selection is canceled, whether programmatically or by user action.
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows) {
//Send the first cell value into textbox'
Txt_FirstName.Text = row.Cells(0).Value.ToString; // or row.Cells["ColumnName"].Value;
}
}
}
MSDN and MSDN

Categories

Resources