datagridview过滤器/排序数据

时间:2020-05-18 00:02:11

标签: c# winforms datagridview

我需要帮助来过滤和排序DataGridView中的信息。
我正在将Excel信息添加到DataGridView。数据应按名称从A到Z进行排序,并按AgeBreedSex进行过滤,因为狗名显示在指定过滤器的列表框中。

private void Button7_Click(object sender, EventArgs e)
{
    Microsoft.Office.Interop.Excel.Application xlApp;
    Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
    Microsoft.Office.Interop.Excel.Range xlRange;

    int xlRow;
    string strfileName;

    openFileDialog1.Filter = "Excel Office | *.xls; *.xlsx";
    openFileDialog1.ShowDialog();
    strfileName = openFileDialog1.FileName;

    if(strfileName != string.Empty)
    {
        xlApp = new Microsoft.Office.Interop.Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open(strfileName);
        xlWorkSheet = xlWorkBook.Worksheets["Sheet1"];
        xlRange = xlWorkSheet.UsedRange;
        int i = 0;
        for(xlRow = 2; xlRow <=xlRange.Rows.Count; xlRow++)
        {
            i++;
            dataGridView1.Rows.Add(i, xlRange.Cells[xlRow,1].Text, xlRange.Cells[xlRow, 2].Text, xlRange.Cells[xlRow, 3].Text, xlRange.Cells[xlRow, 4].Text, xlRange.Cells[xlRow, 5].Text, xlRange.Cells[xlRow, 6].Text);      
        }
    xlWorkBook.Close();
    xlApp.Quit();
    }
}

enter image description here

0 个答案:

没有答案