问题
I'm working in C# project, I made a GridView and I already made a Button in one column. When I press the Button I want to open Dialog form, I did experimental code showing a message when I press it, the experimental code as below:
void dataGridViewShowCourse_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//make sure click not on header and column is type of ButtonColumn
if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
{
//TODO - Execute Code Here
MessageBox.Show("I pressed CellButton");
}
}
And to call CellContentClick I did this code:
public void showCourse()
{
SqlCommand com = new SqlCommand(@"SELECT [Course_ID]
,[CourseCreated].[CourseCreated_Name]
,[CourseCreated].[CourseCreated_Type]
,[Course_Hours]
,[Course_Duration]
,[Course_Place]
,[Trainer_Info].[Trainer_Name]
FROM [VolunteersAffairs].[dbo].[Course_Info],[CourseCreated], [Trainer_Info]
where [Course_Info].[CourseCreated_ID] = [CourseCreated].[CourseCreated_ID] and [Trainer_Info].[Trainer_ID] = [Course_Info].[Trainer_ID]", con);
SqlDataAdapter da = new SqlDataAdapter();
DataSet dats = new DataSet();
da.SelectCommand = com;
da.Fill(dats, "Course_Info");
dataGridViewShowCourse.DataSource = null;
dataGridViewShowCourse.Columns.Clear();
dataGridViewShowCourse.DataSource = dats.Tables["Course_Info"];
DataGridViewButtonColumn col = new DataGridViewButtonColumn();
col.UseColumnTextForButtonValue = true;
col.Text = "ِِAdd";
col.Name = "stedents";
col.HeaderText = "Got Course";
dataGridViewShowCourse.Columns.Add(col);
//Here is my Event Handler
dataGridViewShowCourse.CellContentClick += new DataGridViewCellEventHandler(dataGridViewShowCourse_CellContentClick);
}
When I press that button for first time every thing is worked fine, But the problem is that when I add some rows the code executed many times, meant that the message appears one time for first time before adding any row, and if I add one more row the message appears twice, three times...etc. Or when I press the first row the message appears one time, second row the message appears twice, third row the message appears three time....etc.
Please tell me me where is the error.
Note* the function showCourse() called here:
public AddCourses()
{
InitializeComponent();
showCourse();
FillComboBoxCourseName();
comboBoxCourseName.SelectedItem = null;
FillComboBoxTrainerName();
comboBoxTrainers.SelectedItem = null;
}
And here is the button where I call again the ShowCourse()
private void buttonAddCourse_Click(object sender, EventArgs e)
{
try
{
CourseClass c = (CourseClass)comboBoxCourseName.SelectedItem;
//SqlCommand com = new SqlCommand("update Course_Info set Course_Hours=\' \'where Course_ID = " + c.ID, con);
SqlCommand com = new SqlCommand(@"INSERT INTO [VolunteersAffairs].[dbo].[Course_Info]
([CourseCreated_ID]
,[Course_Hours]
,[Course_Duration]
,[Course_Place]
,[Trainer_ID])
VALUES
(@CourseCreatedID
,@CourseHours
,@CourseDuration
,@CoursePlace
,@TrainerID)", con);
com.Parameters.Add("@CourseCreatedID", SqlDbType.Int).Value = (comboBoxCourseName.SelectedItem as CourseClass).ID;
com.Parameters.Add("@CourseHours", SqlDbType.NVarChar, 50).Value = textBoxCourseHours.Text;
com.Parameters.Add("@CourseDuration", SqlDbType.NVarChar, 50).Value = textBoxCourseDuration.Text;
com.Parameters.Add("@CoursePlace", SqlDbType.NVarChar, 50).Value = textBoxCoursePlace.Text;
com.Parameters.Add("@TrainerID", SqlDbType.Int).Value = (comboBoxTrainers.SelectedItem as CourseClass).ID;
con.Open();
com.ExecuteNonQuery();
con.Close();
showCourse(); // Here is the Function
FillComboBoxCourseName();
}
catch (Exception ex)
{
if (con.State == ConnectionState.Open)
con.Close();
MessageBoxEx.Show("Please Try Again", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
回答1:
In your event handler code you are re-assigning the event handler again and again...
void dataGridViewShowCourse_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// Problem is in this line, re-assigning the same event handler again to the same event
// event handler will execute more than 1 time and number of execution will also keep increasing each time
dataGridViewShowCourse.CellContentClick += new DataGridViewCellEventHandler(dataGridViewShowCourse_CellContentClick);
//make sure click not on header and column is type of ButtonColumn
if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
{
//TODO - Execute Code Here
MessageBox.Show("I pressed CellButton");
}
}
you have the following line of code in your "showCourse" method, therefore you don't need to assign it again
dataGridViewShowCourse.CellContentClick += new DataGridViewCellEventHandler(dataGridViewShowCourse_CellContentClick);
To solve the problem you need to assign the event handler in another function as below
public void showCellButton()
{
dataGridViewShowCourse.CellContentClick += new DataGridViewCellEventHandler(dataGridViewShowCourse_CellContentClick);
}
来源:https://stackoverflow.com/questions/20036051/handle-cell-button-in-gridview-in-c-sharp