最近用C#的winform開發了一個項目,從中積累了一些各個控件的使用方法,特將部分方法共享。
這一篇的主角是DataGridView:
屬性組如下:
1、背景顏色設置
2、是否一次性支持選擇多條記錄
3、選擇模式,一點擊就是選擇一行
4、綁定數據源后的隱藏列屬性

this.dataGridView.Columns["emp_photo"].Visible = false;
5、綁定數據源后的凍結列屬性,使用戶在使用橫向條查看數據時,也能時刻看到是誰的數據,提高用戶體驗
凍結了第一列的數據

this.dataGridView.Columns[0].Frozen = true;
方法組如下:
1、DataGridView控件的標題列設置序號,同時設置默認寬度

private void dView_employee_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { dView_employee.RowHeadersWidth = 60; for (int i = 0; i < dView_employee.Rows.Count; i++) { int j = i + 1; dView_employee.Rows[i].HeaderCell.Value = j.ToString(); } }
效果如圖1所示:
2、datagirdview的行雙擊事件

private void dView_employee_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { /*判斷點擊行標題與列標題*/ if (e.RowIndex > -1 && e.ColumnIndex > -1) { /*傳員工工號過去*/ emp_id = dView_employee.CurrentRow.Cells[0].Value.ToString(); Employee_file file = new Employee_fil(); file.Show() //this.Hide(); } }
三種點擊方法的比較:
三種方法,我個人都用過,最后還是由衷地覺得CellDoubleClick方法是比較符合我現在的這個需求的:
CellClick方法是當用戶一點擊,就執行事件,有時用戶不經意的點擊都會觸發執行事件;
CellContentClick方法是當用戶點擊DataGirdView單元格中的內容時,才會觸發該事件,用戶在使用時,有時他不能准確地點擊到單元格內容,導致執行效果老是出不來;
CellDoubleClick方法是當用戶雙擊某一行的數據時,執行事件,配合選擇模式(SelectionMode)屬性,效果不錯。
3、DataGirdView中的數據轉化為DataTable

public DataTable GetDgvToTable(DataGridView dgv) { DataTable dt = new DataTable(); for (int count = 0; count < dgv.Columns.Count; count++) { DataColumn dc = new DataColumn(dgv.Columns[count].Name); dt.Columns.Add(dc); } for (int count = 0; count < dgv.Rows.Count; count++) { DataRow dr = dt.NewRow(); for (int countsub = 0; countsub < dgv.Columns.Count; countsub++) { dr[countsub] = Convert.ToString(dgv.Rows[count].Cells[countsub].Value); } dt.Rows.Add(dr); } return dt; }
4、DataGirdView中的數據導出到Excel,其中調用了上次轉化成DataTable的方法,用的是網上導出到Execl的方法,但同時增加了對數字與時間字段的判斷

public void ExportTOExcel(DataGridView dView) { /*用的三層結構,調用了一個類*/ using (BLLConnect a = new BLLConnect()) { DataTable myDt = GetDgvToTable(dView); SaveFileDialog saveFileDialog = new SaveFileDialog(); if (myDt.Rows.Count == 0) { MessageBox.Show("沒有數據可供導出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { saveFileDialog.Filter = "Execl文件|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.Title = "導出文件保存路徑"; saveFileDialog.FileName = null; saveFileDialog.ShowDialog(); string FileName = saveFileDialog.FileName; if (FileName.Length != 0) { DataTable dt = myDt; FileStream objFileStream; StreamWriter objStreamWriter; string strLine = ""; objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write); objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode); for (int i = 0; i < dt.Columns.Count; i++) { strLine = strLine + dt.Columns[i].ColumnName.ToString() + Convert.ToChar(9); } objStreamWriter.WriteLine(strLine); strLine = ""; for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { if (j == 0 || j == 7 || j == 15 || j == 17) /*數字*/ { strLine = strLine + "@" + Convert.ToString(dt.Rows[i][j]) + Convert.ToChar(9); } else if (j == 10 || j == 13) /*時間*/ { strLine = strLine + string.Format("{0:d}", Convert.ToDateTime(dt.Rows[i][j])) + Convert.ToChar(9); } else { strLine = strLine + Convert.ToString(dt.Rows[i][j]) + Convert.ToChar(9); } } objStreamWriter.WriteLine(strLine); strLine = ""; } objStreamWriter.Close(); objFileStream.Close(); MessageBox.Show("數據導出成功","提示"); } } } }