简体   繁体   English

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

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

I'm new developing and I need a detailed answered, I'm sorry for my bad english... I will try to explain myself the best I can.. I've 2 tables in Mysql 我是新手,我需要详细的答复,对不起我的英语不好。。。我会尽力向自己解释一下。.我在Mysql中有2个表

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

In my application I use all the information inside table1 to fill a DataGridView, also I've added a comboBox column, the combobox displays "status_name" from table2, I want to modify ticket_status with the information contained in the comboBox, how can I do that? 在我的应用程序中,我使用table1内的所有信息来填充DataGridView,还添加了一个comboBox列,组合框显示table2中的“ status_name”,我想使用comboBox中包含的信息来修改ticket_status,我该怎么办那?

this is my code: 这是我的代码:

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

} }

I'm not able to post Images :( 我无法发布图片:(

You have to add DataGridView.CellValueChanged Event . 您必须添加DataGridView.CellValueChanged Event Function assigned to that event will be executed when you change any cell value. 更改任何单元格值时,将执行分配给该事件的功能。

// 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

}

In event handler you should check what column/cell was changed (row and cell index should be passed thru DataGridViewCellEventArgs e argument to your method that handles CellValueChanged event). 在事件处理程序中,您应该检查更改了哪些列/单元格(行和单元格索引应通过DataGridViewCellEventArgs e参数传递给处理CellValueChanged事件的方法)。

After that check - you should "do your update". 检查之后-您应该“进行更新”。

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

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