按标题单击排序dgv

rjee0c15  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(226)

目前我的系统是通过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();
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题