只有来自DataGridView第一行的数据显示为另一个表单[C#]

时间:2014-12-21 12:31:31

标签: c# mysql datagridview

我有一个以另一种形式设置的dataGridView。我正在尝试使用在dataGridView中选择的UID将信息从MySQL数据库检索到另一个表单。

即使选择了其他用户,它也只显示dataGridView中第一个选定项目的信息。这是我的编码:

        try // First selects the column Client_ID from the database
        {
            using (MySqlConnection secConn = new MySqlConnection(strConnect))
            using (MySqlCommand selCmd = new MySqlCommand())
            {
                selCmd.CommandText = @"SELECT Client_ID FROM cpr_clients";
                selCmd.Connection = secConn;
                secConn.Open();

                MySqlDataAdapter aDapter = new MySqlDataAdapter();
                aDapter.SelectCommand = selCmd;
                DataTable dTable = new DataTable();
                aDapter.Fill(dTable);
                admin.gridClients.DataSource = dTable;

                secConn.Close();

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        try
        {
            // Checks which row is selected and displays value of selected cell into textbox
            string cRow = admin.gridClients.CurrentRow.Cells[0].Value.ToString();
            string Query = "SELECT Admin_Com FROM cpr_clients WHERE Client_ID='" + cRow + "';";
            MySqlConnection myConn = new MySqlConnection(strConnect);
            MySqlCommand myCmd = new MySqlCommand(Query, myConn);
            MySqlDataReader myReader;
            myConn.Open();
            myReader = myCmd.ExecuteReader();
            while (myReader.Read())
            {
                txtAdminCom.Text = myReader["Admin_Com"].ToString();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

0 个答案:

没有答案