繁体   English   中英

c#在DataGridView中使用ComboBox修改Mysql中的单元格

[英]c# Modify a Cell in Mysql using a ComboBox in a DataGridView

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

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

在我的应用程序中,我使用table1内的所有信息来填充DataGridView,还添加了一个comboBox列,组合框显示table2中的“ status_name”,我想使用comboBox中包含的信息来修改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());
        }

}

我无法发布图片:(

您必须添加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事件的方法)。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM