How do I fire up CellContentDoubleClick in C#? - c#

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.

Related

Get selected row in DataGridView

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;
}

How to excute some code only for the first asp:button click

As I'm a newbie to asp.net I need to know whether there is anyway that I can execute some code only for the first button click for a asp:button in a web page.
Actually my requirement is that I need to enter checked values of a asp:check box list to database for the click event of a asp:button. I need to do some object creations only for the first time. Then I just need to add values to the database. How can I identify the first button click? If there is more efficient way than this?
protected void Button1_Click(object sender, EventArgs e)
{
if (Session["Clicked"] == null)
Session["Clicked"] = true;
else {
// We already ran this function once, so do other stuff from now on
...
return;
}
// Code below this comment will be executed only on the 1st button click
...
}

How do you PerformClick(); for a button on a different tab?

I'm using Visual c# express 2010, I have 3 tabs and on the first tab there is a button that exits the program. I'm trying to call that button click on the 2nd and 3rd tab with
btnExit.PerformClick();
but since it isn't visible nothing happens. How would I call the invisible button click?
any help would be appreciated
EDIT:
Thanks for the replies, the two answers work great but I found a way that I think is easier and better.
instead of systematically changing tabs or calling a whole different method, I did this
btnExit_Click(sender, e);
I can put that in any other button click and it works great, very simple to.
I think it's better to create a method that actually has the code to exit the program, and call that method from btnExit click event and also other buttons click event, than PerformClick of the exit button.
void ExitApplication()
{
// code to exit the application
}
protected void btnExit_Click(object sender, EventArgs e)
{
ExitApplication();
}
protected void ButtonInOtherTab_Click(object sender, EventArgs e)
{
ExitApplication();
}
This way it's easier to read and understand.
myTabs.SelectedTab = specificTab;
btnExit.PerformClick();

ButtonClick in DropDown not working in VS2010/Outlook2010

I'm working on an add-in for Outlook 2010. I've created a new Ribbon Tab with a dropdown box. I'd like for the data in the dropdown to update when I click the dropdown button, but nothing is occurring. I've written the sample code below for testing purposes and nothing fires:
private void dropDown1_ButtonClick(object sender, RibbonControlEventArgs e)
{
MessageBox.Show("Dropdown CLick");
}
This is as simple as it gets, but nothing is firing. Any ideas of what I might be doing wrong? The only other code that exists in this ribbon right now is what loads the data.
Thank you
From my understanding
private void dropDown1_Click(object sender, RibbonControlEventArgs e)
{
MessageBox.Show("Dropdown CLick");
}
is what you wanna do. No where did I see ButtonClick as the proper tag.

How do I handle an event in C# based on a cell click in a DataGridView?

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());
}

Categories

Resources