简体   繁体   中英

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

 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?

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 . 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).

After that check - you should "do your update".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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