当前,我的系统通过mysql数据库连接,以将数据从mysql获取到dgv。 我需要一个可以在单击标题时与当前代码混合以对数据进行排序的代码。对不起,英语不好。
private void btnDisplay_Click(object sender, EventArgs e)
{
string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString; //Set your MySQL connection string here.
string query = "SELECT lrn,first_name,middle_name,last_name,grade_and_section,student_gender,student_guardian,contact_number FROM student_info;"; // set query to fetch data "Select * from tabelname";
using (MySqlConnection conn = new MySqlConnection(connString))
{
using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn))
{
DataSet ds = new DataSet();
adapter.Fill(ds);
dataStudent.DataSource = ds.Tables[0];
dataStudent.Columns[0].HeaderCell.Value = "LRN";
dataStudent.Columns[1].HeaderCell.Value = "First Name";
dataStudent.Columns[2].HeaderCell.Value = "Middle Name";
dataStudent.Columns[3].HeaderCell.Value = "Last Name";
dataStudent.Columns[4].HeaderCell.Value = "Grade And Section";
dataStudent.Columns[5].HeaderCell.Value = "Gender";
dataStudent.Columns[6].HeaderCell.Value = "Guardian";
dataStudent.Columns[7].HeaderCell.Value = "Contact Number";
dataStudent.Columns[5].Visible = false;
dataStudent.Columns[6].Visible = false;
dataStudent.Columns[7].Visible = false;
}
}
}
private void dataStudent_CellClick(object sender, DataGridViewCellEventArgs e)
{
int index = e.RowIndex;
DataGridViewRow selectedRow = dataStudent.Rows[index];
txtLRN.Text = selectedRow.Cells[0].Value.ToString();
txtFName.Text = selectedRow.Cells[1].Value.ToString();
txtMName.Text = selectedRow.Cells[2].Value.ToString();
txtLName.Text = selectedRow.Cells[3].Value.ToString();
txtYear.Text = selectedRow.Cells[4].Value.ToString();
txtGender.Text = selectedRow.Cells[5].Value.ToString();
txtGuardian.Text = selectedRow.Cells[6].Value.ToString();
txtContact.Text = selectedRow.Cells[7].Value.ToString();
}