2017-04-11 32 views
0

我想知道如何從mssql中快速檢索datagridview中的數據。就像我運行我的程序時一樣,我的網格開始填充數據,而且速度很慢,就像我可以看到填充過程一樣,只需要2秒鐘,但我希望它能像納秒一樣快速完成。任何人都會讓我知道如何做到這一點。銷售系統如何從MSSQL快速檢索DataGridView中的數據

私人無效FillGrid() {

 DataTable xTable = new DataTable(); 
     new SqlDataAdapter("select * from tblProducts order by BID DESC",xConn).Fill(xTable); 
     DGV1.DataSource = xTable; 
     DGV1.Columns[0].Width = 50; 
     DGV1.Columns[1].Width = 100; 
     DGV1.Columns[2].Width = 150; 
     DGV1.Columns[3].Width = 50; 

     for (int i = 0; i < DGV1.Rows.Count-1; i++) 
     {     
      int QTY = Int32.Parse(DGV1.Rows[i].Cells[4].Value.ToString()); 
      if(QTY >=21 && QTY <= 50) 
      { 
       DGV1.Rows[i].Cells[4].Style.BackColor = Color.Yellow; 
       DGV1.Rows[i].Cells[4].Style.Font = new Font("Arial", 11, FontStyle.Bold); 
      } 
      else if (QTY >= 0 && QTY <= 20) 
      { DGV1.Rows[i].Cells[4].Style.BackColor = Color.Red; 
      DGV1.Rows[i].Cells[4].Style.Font = new Font("Arial", 11, FontStyle.Bold); 
      } 

      { DGV1.Rows[i].Cells[4].Style.BackColor = Color.Green; 
      DGV1.Rows[i].Cells[4].Style.Font = new Font("Arial", 11, FontStyle.Bold); 
      } 
     } 
    } 
+1

您是否想要爲我們提供任何代碼,或者我們是否應該將您指向簡單按鈕?嚴肅地說,沒有任何細節,你如何預期這會產生一個答案? –

+0

什麼是2秒鐘代碼在哪裏? – Krishna

+0

@Krishna代碼發佈 – Jibbo

回答

0

的順便說一下它的C#和項目的桌面應用程序開發基於條碼點據我所知,存儲過程比函數快,使用它們 ! 此外,如果你可以分享代碼或一些場景的細節,所以我可以輕鬆解決你的問題。

+0

薩阿德我張貼您現在可以看到的代碼 – Jibbo

0

有兩件事情可以做

首先 刪除列的寬度在填充,因爲你不需要每次都設置您填寫的網格是不必要的,移動代碼的設計師或這樣做在窗戶設計直接

移動列格式到datagridview的單元格格式如下,本可避免的默認格式,然後你形成兩次

private void DGV1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    int QTY = Int32.Parse(DGV1.Rows[e.RowIndex].Cells[4].Value.ToString()); 
     if(QTY >=21 && QTY <= 50) 
     { 
      DGV1.Rows[e.RowIndex].Cells[4].Style.BackColor = Color.Yellow; 
      DGV1.Rows[e.RowIndex].Cells[4].Style.Font = new Font("Arial", 11, FontStyle.Bold); 
     } 
     else if (QTY >= 0 && QTY <= 20) 
     { 
      DGV1.Rows[e.RowIndex].Cells[4].Style.BackColor = Color.Red; 
      DGV1.Rows[e.RowIndex].Cells[4].Style.Font = new Font("Arial", 11, FontStyle.Bold); 
     } 
     else 
     { 
      DGV1.Rows[e.RowIndex].Cells[4].Style.BackColor = Color.Green; 
      DGV1.Rows[e.RowIndex].Cells[4].Style.Font = new Font("Arial", 11, FontStyle.Bold); 
     } 
}