I have a DataGridView, I want to select the cell of the first column.
Heres is my datagridview.Click method:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
name = dataGridView1.CurrentRow.Cells[0].Value.ToString();
}
Currently my name variable is null.
What am I doing wrong?
CurrentRow may not be set yet, so use the RowIndex property for the event argument. Try it this way:
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
if (e.RowIndex > -1 && dataGridView1.Rows[e.RowIndex].Cells[0].Value != null) {
name = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
}
}
And just in case, make sure the event is wired up:
public Form1() {
InitializeComponent();
dataGridView1.CellClick += dataGridView1_CellClick;
}
You could do this:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
var view = (sender as DataGridView); //<-- notes this
var currentCellString = view.CurrentCell.Value.ToString();
}
Sometimes you need to grab the sender object at use that - because it will always be updated.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
object name = dataGridView1.Rows[e.RowIndex].Cells[0].Value;
MessageBox.Show(name.ToString() == string.Empty ? "myvalue" : name.ToString());
}
Related
I have button inside DataGrid (created by Edit Column), and TextBox same. I have created CellClick event on button. Now I want TextBox value which will be enter by user.
How can I get it?
I've tried
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
//Here i will get cell value which bind from database
dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
}
}
try this
form load:
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
CellClick Event:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row = new DataGridViewRow();
row = dataGridView1.SelectedRows[0];
if (row.Cells[0].Value!=null && row.Cells[1].Value != null && row.Cells[2].Value != null) {
textBox1.Text = row.Cells[0].Value.ToString();
textBox2.Text = row.Cells[1].Value.ToString();
textBox3.Text = row.Cells[2].Value.ToString();
}
}
I have used the CellContentClick and it works perfectly. I've also added a string variable that takes the value. This is my code:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
string value = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
MessageBox.Show(value);
}
}
I made a code to GET the DGV current cell's value(Marked one - a cell inside DGV that has been chosen by a user mouse click ). Now I like to know how to do the SET code (insert TextBox's value into current cell).
This code (GET) works fine:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBxGET.Text = dataGridView1.CurrentCell.Value.ToString();
}
The problem is here where this code (SET) shows the row number (not what I want):
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.CurrentCell.Value = textBoxSET.Text;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
txtselectedcellvalue.Text = dataGridView1.CurrentCell.Value.ToString();
//txtselectedcellvalue.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[dataGridView1.CurrentCell.ColumnIndex].Value.ToString();
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if(dataGridView1.SelectedCells.Count > 0)
txtselectedcellvalue.Text = dataGridView1.CurrentCell.Value.ToString();
//txtselectedcellvalue.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[dataGridView1.CurrentCell.ColumnIndex].Value;
}
private void btnchange_Click(object sender, EventArgs e)
{
dataGridView1.CurrentCell.Value = txtvaluechange.Text;
//dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[dataGridView1.CurrentCell.ColumnIndex].Value = txtvaluechange.Text;
}
This code works fine for me. Also, #Kaj's code works perfect. Please explain your error in proper way.
try this :
dataGridView1.Rows[dataGridView1.CurrentRow.Index]
.Cells[dataGridView1.CurrentCell.ColumnIndex].Value = textBoxSET.Text;
i am making a windows form application in which i used a datagridview.
i want that when i write something in textbox in datagridview,than a messagebox appears containing the string i wrote..
ican't get my text in textchanged event..
all thing must be fired in textchanged event..
here is my code:-
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
TextBox tb = (TextBox)e.Control;
tb.TextChanged += new EventHandler(tb_TextChanged);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
//listBox1.Visible = true;
//string firstChar = "";
//this.listBox1.Items.Clear();
//if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
string str = dataGridView1.CurrentRow.Cells["Column2"].Value.ToString();
if (str != "")
{
MessageBox.Show(str);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
var enteredText = (sender as TextBox).Text
...
}
Showing MessageBox in TextChanged will be very annoying.
Instead you could try it in DataGridView.CellValidated event which is fired after validation of the cell is completed.
Sample code:
dataGridView1.CellValidated += new DataGridViewCellEventHandler(dataGridView1_CellValidated);
void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
}
When I check the checkbox in the checkboxlist, the checkbox.text will add into the listbox.
But when I uncheck the checkbox, the checkbox.text will be removed from the listbox.
But the problem is I do not know how to remove the selected items from the listbox.
For example, when i check the checkbox1, checkbox2, checkbox3, the listbox will display
checkbox1
checkbox2
checkbox3
However, when i uncheck the checkbox, Still Same
I stuck at here. Help !!
here is my code:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked) listBox1.Items.Add(checkBox1.Text);
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked) listBox1.Items.Add(checkBox2.Text);
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked) listBox1.Items.Add(checkBox3.Text);
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (checkBox4.Checked) listBox1.Items.Add(checkBox4.Text);
}
if (checkBox3.Checked)
listBox1.Items.Add(checkBox3.Text);
else
listBox1.Items.Remove(checkBox3.Text);
Note that this will always remove whatever is in the Text property. This means that, if I check the box, change the text in textBoxX, and then uncheck, it will remove a different item.
The Items collection on a ListBox has a Remove method. Put an else in each of your CheckedChanged events and use the Remove method.
if (checkBox4.Checked) listBox1.Items.Add(checkBox4.Text);
else listBox1.Items.Remove(checkBox4.Text);
Make a common function and call. For e.g.
private void addRemove(CheckBox chk)
{
if (chk.Checked)
listBox1.Items.Add(chk.Text);
else
listBox1.Items.Remove(chk.Text);
}
Call
addRemove(checkbox1);
You can do one funciton
private void checkBoxCheckedChanged(object sender, EventArgs e)
{
CheckBox cb = sender as CheckBox;
if (cb != null)
if (cb.Checked) listBox1.Items.Add(cb.Text); else listBox1.Items.Remove(cb.Text);
}
And then add it for all your checkboxes as CheckedChanged event.
Call Remove() method. It accepts one argument that specifies the item to remove.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
listBox1.Items.Add(checkBox1.Text);
else
listBox1.Items.Remove(checkBox1.Text);
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked)
listBox1.Items.Add(checkBox2.Text);
else
listBox1.Items.Remove(checkBox2.Text);
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked)
listBox1.Items.Add(checkBox3.Text);
else
listBox1.Items.Remove(checkBox3.Text);
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (checkBox4.Checked)
listBox1.Items.Add(checkBox4.Text);
else
listBox1.Items.Remove(checkBox4.Text);
}
If thats all you want to do why not assign the same event to every checkbox and do the below:-
CheckBox chkBox=(CheckBox)sender;
if (chkBox.Checked)
listBox1.Items.Add(chkBox.Text);
else
listBox1.Items.Remove(chkBox.Text);
I am trying to clear the contents of a listbox when a new tab is seleced. Here is what I got, but nothing happens.
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabControl1.TabPages["entryTab"])
{
readBox.Items.Clear();
reminderBox.Items.Clear();
}
}
Try something like this in your form load
tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_SelectedIndexChanged);
// Try this set null to DataSource
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabControl1.TabPages["entryTab"])
{
readBox.DataSource = null;
reminderBox.DataSource = null;
}
}