1. 程式人生 > >【C#】DataGridView操作

【C#】DataGridView操作

1、其一

            private const int ROW_HEIGHT = 70;
            private void setDataGridViewControl(int pageIndex, int pageSize)
            {
                pagePTR = pageSize * (pageIndex - 1);
                if (myRows != null)
                {
                    myDTProject.Rows.Clear();
                    
for (int i = 0; i < dgvPager.PageSize; i++) { string myNO = myRows[pagePTR]; DataRow row = myDTProject.NewRow(); row[0] = myNO; row[1] = myManageDB.getDataFromCell("proDepartment", "ProjectManager
", "proNO", myNO); row[2] = myRegexInfo.getMatchedChineseStr(myManageDB.getDataFromCell("proManager", "ProjectManager", "proNO", myNO)); row[3] = myRegexInfo.getMatchedChineseStr(myManageDB.getDataFromCell("proAuthor", "ProjectManager", "proNO", myNO)); row[
4] = myManageDB.getDataFromCell("proEditDate", "ProjectManager", "proNO", myNO).Split(' ')[0]; row[5] = myManageDB.getDataFromCell("proName", "ProjectManager", "proNO", myNO); myDTProject.Rows.Add(row); pagePTR++; if (pagePTR >= this.dgvPager.RecordCount) { break; } } //使用者不能調整列標題高度 dgvProjectManager.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; //隱藏行標題 dgvProjectManager.RowHeadersVisible = false; //設定行高 dgvProjectManager.RowTemplate.Height = ROW_HEIGHT; //設定內邊距大小 Padding newPadding = new Padding(0, 1, 0, ROW_HEIGHT - 35); dgvProjectManager.RowTemplate.DefaultCellStyle.Padding = newPadding; dgvProjectManager.AllowUserToAddRows = false; dgvProjectManager.AllowUserToDeleteRows = false; dgvProjectManager.AllowUserToResizeColumns = false; //設定無單元格邊框 dgvProjectManager.CellBorderStyle = DataGridViewCellBorderStyle.None; //單元格選擇模式為全選 dgvProjectManager.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dgvProjectManager.MultiSelect = false; dgvProjectManager.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders); //設定字型樣式 dgvProjectManager.DefaultCellStyle.SelectionForeColor = Color.Black; dgvProjectManager.DefaultCellStyle.ForeColor = Color.Black; //設定選中行背景色,需要繪製自定義背景應該在RowPrePaint處理,並設定該屬性為Color.Transparent; //dataGridView.RowTemplate.DefaultCellStyle.SelectionBackColor = Color.LightSkyBlue; dgvProjectManager.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; dgvProjectManager.DataSource = myDTProject; //只需要把需要繪製的DataGridView傳入DataGridViewRenderer即可, //這裡的隱藏列即為需要重新繪製的內容 DataGridViewRender render = new DataGridViewRender(dgvProjectManager, dgvProjectManager.Columns[dgvProjectManager.ColumnCount - 1]); } }