【问题标题】:c# Modify a Cell in Mysql using a ComboBox in a DataGridViewc# 使用 DataGridView 中的 ComboBox 修改 Mysql 中的单元格
【发布时间】:2013-03-03 13:26:00
【问题描述】:

我是新手,我需要详细的回答,对不起我的英语不好... 我会尽力解释自己。 我在 Mysql 中有 2 个表

 table1:  id_ticket , ticket_description, ticket_status(id_status)         
 table2 :    id_statu , status_name

在我的应用程序中,我使用 table1 中的所有信息来填充 DataGridView,还添加了一个组合框列,组合框显示 table2 中的“status_name”,我想用组合框中包含的信息修改 ticket_status,如何我可以这样做吗?

这是我的代码:

public void CreateAssignedToMe(string ConnString)
    {
        string assignedTo = textBoxUid.Text;
        string query = "SELECT * FROM reports WHERE ticket_assignee='"+assignedTo+"' AND ticket_resolution < 3;";

        AssignToMe = new AssignedToMe();
        AssignToMe.ConnString = ConnString;
        DataGridView dgvReports = AssignToMe.dataGridViewAssignedToMe;

        try
        {
            MySqlConnection conn = new MySqlConnection(ConnString);
            conn.Open();
            MySqlDataAdapter daUsers = new MySqlDataAdapter(query,ConnString);
            DataSet dsUsers = new DataSet();
            daUsers.Fill(dsUsers,"report");
            dgvReports.DataSource = dsUsers;
            dgvReports.DataMember = "report";
            dgvReports.AllowUserToAddRows = false;
            dgvReports.AllowUserToDeleteRows = false;
            dgvReports.Columns["ticket_departmentResponsive"].Visible = false;
            dgvReports.Columns["ticket_assignee"].Visible = false;


                string queryStatus = "SELECT * FROM status";
                MySqlDataAdapter daStatus = new MySqlDataAdapter(queryStatus, ConnString);                  
                DataSet dsStatus = new DataSet();
                MySqlCommandBuilder builder = new MySqlCommandBuilder(daStatus);
                daStatus.Fill(dsStatus, "Resolution");

                daStatus.UpdateCommand = builder.GetUpdateCommand();

                DataGridViewComboBoxColumn cbbox = new DataGridViewComboBoxColumn();
                BindingSource StatusBindingSource = new BindingSource();
                StatusBindingSource.DataSource = dsStatus;
                StatusBindingSource.DataMember = "Resolution";
                cbbox.HeaderText = "Resolution";
                cbbox.DropDownWidth = 90;
                cbbox.DataSource = StatusBindingSource;
                cbbox.DisplayMember = "status_name";
                dgvReports.Columns.Add(cbbox);



            AssignToMe.ShowDialog();

        }
        catch(MySqlException ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }

}

我无法发布图片:(

【问题讨论】:

  • 我认为我必须使用类似 EventHandler 的东西,但我不知道该怎么做,或者可能使用按钮来更新信息,但我如何才能获得指定的行对于被修改的组合框?

标签: c# mysql datagridview combobox


【解决方案1】:

您必须添加DataGridView.CellValueChanged Event。当您更改任何单元格值时,将执行分配给该事件的函数。

// adding event handler (somewhere after dgvReports initialization)
dgvReports.CellValueChanged += new DataGridViewCellEventHandler(dgvReports_CellValueChanged);

// function that handles event
private void dgvReports_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    // `e` argument will contain e.RowIndex and e.ColumnIndex properties
    // they may be used to determine which particular cell was changed

}

在事件处理程序中,您应该检查更改了哪些列/单元格(行和单元格索引应通过 DataGridViewCellEventArgs e 参数传递给处理 CellValueChanged 事件的方法)。

检查之后 - 你应该“进行更新”。

【讨论】:

  • 感谢您的回答,现在我可以做我想做的事了,只要一个问题,每次我修改一个单元格时,我都必须单击另一个单元格来运行 dgv_Reports_CellValueChanged,有一种方法可以立即运行它,而不点击其他单元格?
  • 我不确定,但可能您必须通过按 Enter(或离开该单元格)来确认单元格编辑。它可以正常工作,因为在您“确认值编辑”(通过输入键)或离开单元格后更改了单元格的值属性。也许可以在选择值之后调用该事件,但我不知道该怎么做。也许这是另一个问题的好主题。
  • 我建议再问一个问题,可能是这样的:“如何在不离开单元格或按 Enter 的情况下更新 DataGridViewComboBoxCell 值?”
猜你喜欢
  • 2018-01-01
  • 2015-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多