DataGridView Cell Editing - c#

Hi
I have a DataGridView which is bound to an XML source.
I have a problem in editing cells. The cell on click becomes selected and when it is edited, by default we overwrite it. My requirement says it should be ready for editing and not selected when clicked.
I want to generate a row dynamically whenever the 'tab' key is pressed.
How can I achieve this?

If I understand you correctly you want the cell to enter edit mode as soon as it is clicked. This can be achieved by setting the EditMode property of the DataGridView to EditOnEnter.
This leaves the text in the editing control selected however, so if you don't want that you could use:
dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
dataGridView1.BeginEdit(false);
}
Can you explain what you mean by adding row dynamically?

With respect to question 1)
You can try this:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.dataGridView1.CellEnter += new DataGridViewCellEventHandler(myDataGrid_CellEnter);
}
void myDataGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if ((this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewTextBoxColumn) ||
(this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn))
{
this.dataGridView1.BeginEdit(false);
}
}

Related

Passing data from datagridview to another

I want to pass rows from datagridview to another in same form.I have numericupdown and i want to choose one row and pass it as many as in my numericupdown value. But in second datagridview i only want to show 1 of them.others will be copied to datatable.
I don't know how to do that.Can anyone help me with this problem?
private void button1_Click(object sender, EventArgs e)
{
if (Dgw1.CurrentRow == null)
return;
foreach (DataGridViewRow row in Dgw1.SelectedRows)
{
((DataTable)Dgw2.DataSource).ImportRow(((DataRowView)row.DataBoundItem).Row);
}
}

Selecting whole row in datagridview

I want to select whole row of DataGridView, when I click on some cell in it.
I added this code in CellMouseDown event
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
dataGridView1.Rows[e.RowIndex].Selected = true;
}
And it doesnt work, when I add this line to CellMouseClick event, it works, but it is slowly, it wait for mouse release, and then select it.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].Selected = true;
}
Any solutions?
There are property in GridView Related Of Mode Of Selecting Row in gridview
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
You Can Reference To This Document https://msdn.microsoft.com/en-us/library/3c89df86(v=vs.110).aspx
Are you sure you select the option Full Row Select into the properties of the Datagrid like this:

C# DataGridView detecting select all button

I'm developing a C# Windows Form program and I've implemented a DataGridView on it. Now, after setting the data source, when I click the top left button on the datagridview, it selects all rows, just like Microsoft Excel. However, I dynamically hide and show rows on it, and after clicking that button I realized that it also selects the invisible ones. I don't want to implement "SelectionChanged" event because I constantly select some rows and normally I can't select the invisible ones. Only this button selects it. I'm looking for an event like this:
datagridView1_SelectAllClicked(object sender, EventArgs e)
{
// do stuff
}
Something like this will also work since I don't have to check all selections:
dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if(dataGridView1.IsSelectAllCells())
{
// do stuff
}
}
If I have to, I will add the event to deselect the invisible rows, but I prefer some solution like the first one. Any advices? Thanks in advance.
Edit: I'm checking "dataGridView1.SelectedRows" property on button clicks only, not after the selection was made. So, some function that I can implement to button click events will also solve my problem.
The DataGridView class provides the AreAllCellsSelected methode:
Returns a value indicating whether all the DataGridView cells are currently selected. (MSDN)
With that we can get a solution like your second one:
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
DataGridView view = sender as DataGridView;
if (view.AreAllCellsSelected(true))
{
foreach (DataGridViewRow row in view.Rows)
{
//deselect all invisible rows
if (!row.Visible)
row.Selected = false;
}
}
}
I've managed to solve it finally, the solution was simpler than I expected.
private void RemoveInvisibleSelection()
{
if (dataGridView1.SelectedRows.Count == dataGridView1.Rows.Count)
{
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
if (!dataGridView1.SelectedRows[i].Visible)
dataGridView1.SelectedRows[i--].Selected = false; // decreased the index value since SelectedRows property loses an object
}
}

C# datagridview mouse click event only certain column

I have working code on a datagridview mouse click event but ı want to working this code clicking only third columns of datagridview . Can you help me
Although you haven't provided any details, maybe this code helps you to get some hints:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2) // third-column
{
// when third cell clicked
}
}

c# dataGridView current cell in some function

Hello and sorry for my English.
dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
This function has parameter DataGridViewCellEventArgs e, with whose help i can find out clicked cell:
dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()
But im writing function for Word Export:
private void WordExport_Click(object sender, EventArgs e)
Which work when i click on button. In this function i need to know current cell, same as in
dataGridView1_CellClick function - dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()
How can i get it?
DataGridView has property CurrentCell which is a reference to currently selected cell (can be null!). So to use it in your word export event do following:
private void WordExport_Click(object sender, EventArgs e)
{
if (dataGridView1.CurrentCell == null) //no cell is selected.
{
return;
}
var value = dataGridVIew1.CurrentCell.Value.ToString();
//or if you always want a value from cell in first column
var value = dataGridVIew1.CurrentCell.OwningRow.Cells[0].Value.ToString()
}
hope that helps you out. Good luck :)

Categories

Resources