could you please inform me how to display the row number of data grid view on label or text box to be able to use it in update or delete table.
I tried the following but it display nothing when i click on the data grid view cells
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int rowIndex = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[rowIndex];
textBox9.Text = dataGridView1.Rows[0].Cells[1].Value.ToString();
}
I use this:
private void tableConteudo_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1)
{
_myObjectVar = (MyObject)table1.Rows[e.RowIndex].DataBoundItem;
}
}
So, instead of using a CellContentClick, you can use CellClick event.
try this,
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
textBox9.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
}
Related
I have a textbox and a datagridview with checkbox. The excel import into datagridview. I add the checkbox into datagridview. I want to input the number into textbox and this number will match the column in datagridview.
please help me!
private void TextBox2_TextChanged(object sender, EventArgs e)
{
}
private void TextBox2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode==Keys.Enter)
{
button4.Focus();
Button4_Click(sender,e);
textBox2.Focus();
}
}
private void Button4_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[3].Value.ToString() == textBox2.Text)
{
row.Cells[0].Value = ((CheckBox)dataGridView1.Controls.Find("DataGridViewCheckBoxCell", true)[0]).Checked;
}
}
}
private void Form9_Load(object sender, EventArgs e)
{
TextBox textBox = new TextBox();
textBox.TextChanged += new EventHandler(TextBox2_TextChanged);
}
Set this property when setting up your DataGridView
dataGridView1.Columns[0].ValueType = typeof(CheckState);
And then in your Event Handler set the cell using this
if (row.Cells[3].Value.ToString() == textBox2.Text)
{
row.Cells[0].Value = CheckState.Checked;
}
I'm currently developing a C# project that displays the users' orders.
Now, being that I am new to this I'm asking you to assist me on how I could possibly access the two buttons from within the DataGridView and give them separate functions.
Where I've got at code wise:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (vHisOrd_BtnDelivered.Selected)
{
}
else if (vOrdHis_Btn.Selected)
{
}
}
DataGridView Image:
You can add DataGridViewButtonColumn to the datagridview and use the cellcontentclick event
Use e.ColumnIndex to determine what code to execute
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
e.RowIndex >= 0)
{
//TODO - Button Clicked - Execute Code Here
}
}
use ColumnIndex inteads of row name
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
//use index in your gridView
if (dgv.Columns[e.ColumnIndex] == 2)
{
//vHisOrd_BtnDelivered
}
else if (dgv.Columns[e.ColumnIndex] == 3)
{
//vOrdHis_Btn
}
}
I have a DataGridView with cells containing strings. If a cell content is changed, I want to change the background of this cell. What event is the best for doing this?
I first tried the CellValueChanged event, but this is even called by clicking this cell without editing the content.
Here is my function code:
private void GVCrs_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
this.GVCrs.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Orange;
}
Regards
R4z0R
You can try CellBeginEdit and CellEndEdit events,
string beforeValue = "";
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
beforeValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() != beforeValue)
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Red;
beforeValue = "";
}
}
Result;
Hope helps,
i had a gridview which has 2 columns , one is textbox column and other is checkbox column, how to know which checkbox is checked .
As shown in image ,suppose any of the checkbox is checked , i want to display that the corresponding text box value to that checkbox.
can anyone help me?i tried the below code , but problem which i am facing is that , the values is getting displayed once i clicked to next checkbox then the previously checked checkbox values is getting displayed..
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
object tempObj = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
dataGridView1_CurrentCellDirtyStateChanged(sender, e);
if (((e.ColumnIndex) == 1) && ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value))
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
}
}
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (((e.ColumnIndex) == 1) && ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value))
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
}
}
these below links helped me to understand the concept of cellvalue_changed and cell_content_click..
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx
and by the help of these links i finally got the solution to my problem
it is as simple as this
//replace the row number and column name with your own
if ((bool)dataGridView1.Rows[0].Cells["Column1"].Value)
{
//do your work
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex > -1)
label1.Text = dataGridView1.Rows[e.RowIndex].Cells["Col1"].Value.ToString();
}
private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == *someIndex*)
{
DataGridViewCheckBoxCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
if (cell != null)
{
if (cell.EditingCellValueChanged)
{
//CheckBox has been clicked
}
//here how to get the checkBoxCell value
var cellChecked = cell.EditingCellFormattedValue;
}
}
}
In my form application, there is a (buttonNEW) that selects NewIndexRow of DataGridView and I want to change index of datagridview with this button.
private void buttonNew_Click(object sender, EventArgs e)
{
if (dataGridView.CurrentRow.Index!=dataGridView.NewRowIndex)
{
dataGridView.ClearSelection();
dataGridView.Rows[dataGridView.NewRowIndex].Selected = true;
label1.Text = dataGridView.CurrentRow.Index.ToString();
}
}
But after clicking the button the index of DataGridView does not change.
What is the problem?
This should work :-
int numofRows = dataGridView.rows.count;
dataGridView.CurrentCell = dataGridView.Rows[numofRows - 1].Cells[0];
Or I think you could also do this :-
dataGridView.CurrentCell = dataGridView.Rows[dataGridView.NewRowIndex].Cells[0];
Try with Linc:
private void buttonNew_Click(object sender, EventArgs e)
{
dataGridView.Rows.OfType<DataGridViewRow>().Last().Selected = true;
}