ALL,
I have a DataGridView control on my WinForms application with the selection property as "Entire Row" and no multi-selection. I also attach the SelectionChanged delegate to it, where I need to get the currently selected row.
private void order_SelectionChanged(object sender, EventArgs e)
{
ordersItemIndex = order.SelectedRows[0].Index;
}
Problem is, when the program starts, there should be no selection at all and only later user can change the selection with mouse or keyboard. So in my Form_Load() event I have this:
order.ClearSelection();
However, this code path throws an exception on the start-up of the program.
Is there a nice way to tell the program "We are loading the form, don't call the delegate", without any additional variable?
Thank you.
You can add your order_SelectionChanged after you clear selection in Form_Load() (not in InitializeComponents())
But better check in your handler is there any selected rows.
private void order_SelectionChanged(object sender, EventArgs e)
{
if (order.SelectedRows.Count > 0)
ordersItemIndex = order.SelectedRows[0].Index;
}
Related
I'm using VS 2010 C#.
I have a form that only has a grid connected to a table. Basically, my idea is to select a row by double clicking it. Then later copy the ID and close the form to proceed to another form. But it is not working as per my test on doubleclick event on grid. It is suppose to show a Message box but it is not triggering.
I'm still new on C# and I've browsed the net for similar problem but most of the example are in VB, there was even one suggestion for me to make dgv a readonly=false (implemented on code).
Here is my code:
...
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'cISDataSet.tbl_Person' table. You can move, or remove it, as needed.
this.tbl_PersonTableAdapter.Fill(this.cISDataSet.tbl_Person);
this.dataGridView1.ReadOnly = false;
}
private void DataGridView1_CellContentDoubleClick(Object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("test");
}
...
I do not have any syntax error on my program.
Add this line to your form1_load. (after this.dataGridView1.ReadOnly = false;)
dataGridView1.CellContentDoubleClick += DataGridView1_CellContentDoubleClick;
You only need to tell the data grid view where to go when a double click happens.
I am having trouble getting my application to work correctly. I am trying to select a row in a datagridview with the mouse. I need to save the index of this row to allow me to navigate around the selected row.
I have been looking at DataGridView.CellMouseClick Event (Link) But I am unable to ensure that the event handler is associated with the CellMouseClick event.
My code for this so far is simple, Im just trying to see if its detecting mouse clicks:
public event DataGridViewCellMouseEventHandler CellMouseClick;
private void DataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)
{
MessageBox.Show("Mouse clicked in the datagridview!");
}
Can anyone point out where I may be going wrong. Any help would be great!
You need to "wire up" the event.
If your DataGridView is called DataGridView1 then you need the following line of code in either the constructor for your form, the designer (if you add the event handler via the designer) or in the Load event:
DataGridView1.CellMouseClick += DataGridView1_CellMouseClick;
This attaches the event handler in your code to the event.
Your current sample looks like this:
public event DataGridViewCellMouseEventHandler CellMouseClick;
private void DataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)
{
MessageBox.Show("Mouse clicked in the datagridview!");
}
There is no need to redeclare the event (public event DataGridViewCellMouseEventHandler CellMouseClick;) unless you are building your own user control that will host a DataGridView and you effectively want to "wrap" or "rebroadcast" that event.
using mysql I feed combobox2 with selection of combobox1.It works OK. but the problem is the first select seems does not trigger the eventhandler. the second time I do it it will trigger.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.SelectedValueChanged += new EventHandler(comboBox1_selectedvaluechanged);
}
private void comboBox1_selectedvaluechanged(object sender, EventArgs e)
{
region = comboBox1.SelectedItem.ToString();
values_to_venue();
db.connection.Close();
}
It's because you're not creating your event handler until the SelectedIndex of the combobox changes. This is fired alongside SelectedValue changes. Create the event handler in the load method ensuring it is there when the first SelectedValue changes. If you put it in the Load make sure you clean it up with -= in the unload. Or, you can just create it in the constructor and then you won't need to remove it.
You do want to set the events in your constructor or load method; also, I think the event you want to use is ComboBox.SelectionChangeCommitted because if the user navigates the DropDown list using the up/down arrows on the keypad, SelectedIndexChanged will fire before the selection is committed - is that what you want??????
I'd like give focus to a textBox after a Tab has been selected but no matter what I try it doesn't work. I've looked at similar questions here but they don't get me the results I need. Here is what Ive tried.
private void tabBDERip_Click(object sender, EventArgs e)
{
textBoxPassword.Focus();
}
and
private void tabAll_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabAll.SelectedTab == tabBDERip)
{
textBoxPassword.Focus();
}
}
Can someone please tell me what I'm doing wrong?
Thanks
First thing the Click event of the TabPage control fires when the user clicks inside the TabPage not on the header so your SelectedIndexChanged event is the one you want to use.
I just tested code very similiar to yours:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabPage2)
{
textBox4.Focus();
}
}
And it worked fine.
Is the password textbox not enabled or something like that?
If you try to call Focus() on a different control does that also not work?
If you set a breakpoint inside the SelectedIndexChanged code does it get hit?
Update: Interesting. If the breakpoint isn't getting hit (before the if) I would double check that your eventhandler is properly attached. Look in your designer.cs for something like:
this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
Update: I put my working example at http://www.ccswe.com/temp/SO_TextBoxFocus.zip maybe looking at it will help you figure out where the issue is.
Update: The easier way to attach an event handler to a control on your form:
1: Select the Control to want to attach an event handler to and then click the Events icon (lightning bolt) in the Properties window.
alt text http://www.ccswe.com/temp/Attach_EventHandler_1.png
2: Find the event you want to attach to and double click to the right.
alt text http://www.ccswe.com/temp/Attach_EventHandler_2.png
3: A code stub will be automatically generated for you and the event will be attached in the designer.
alt text http://www.ccswe.com/temp/Attach_EventHandler_3.png
If you look at the properties window again you'll now see the name of the method that was generated.
FYI, I looked at existing postings and I did not find exactly what I needed.
I am using Visual Studio 2008 to write a C# Windows Forms program. I have a DataGridView object that displays data from a table. When a user clicks on a cell I want to be able to grab the contents of that cell.
How do get an action to take place whenever there is a click on any cell in the datagridview. If the user clicks on the same cell 5 times in a row, I want the action to happen five times.
I am not even sure what the name of the event handler would be.
I tried the following, but it didn't work.
This was the code on the FormName.cs file:
private void DATASOURCEDataGridView_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
MessageBox.Show("clicked");
}
This was the code on the FormName.Designer.cs File:
this.DATASOURCEDataGridView.CellContentClick +=
new System.Windows.Forms.DataGridViewCellEventHandler(
this.DATASOURCEDataGridView_CellContentClick);
Thanks for the help!
Use the CellClickedEvent:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}