您好,登錄后才能下訂單哦!
GridView控件,用表格的形式將數(shù)據(jù)顯示出來。那么如何將將數(shù)據(jù)綁定到GridView中呢?目前值學(xué)習(xí)了兩種,一種是直接綁定數(shù)據(jù)源,另一種是動(dòng)態(tài)顯示數(shù)據(jù)(主要使用對(duì)象有SqlCommand,SqlDataAdapter,DateSet)。這里主要說一下后一種方法:
private void showData(String sql) { try { //自動(dòng)生成表頭 this.gridSend.AutoGenerateColumns = true; //這個(gè)屬性是當(dāng)你點(diǎn)擊GridView中任意一個(gè)單元格時(shí),默認(rèn)選中該單元格所在行 this.gridSend.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //簡(jiǎn)單的數(shù)據(jù)庫(kù)連接 SqlConnection conn = new SqlConnection("server=.\\sqlexpress;database=dodoo2;uid=s1;pwd=s1"); conn.Open(); SqlCommand cmd = new SqlCommand(); SqlDataAdapter da = new SqlDataAdapter(); cmd.Connection = conn; cmd.CommandText = sql; cmd.CommandType = CommandType.Text; da.SelectCommand = cmd; DataSet ds = new DataSet(); da.Fill(ds, "lb_billflow"); this.gridSend.DataSource = ds.Tables[0]; conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message+sqlSel); } }
顯示出數(shù)據(jù)后,通過CellClick()事件,獲取選中行的數(shù)據(jù),例如獲取選中行中的id值:
int id = Convert.ToInt16(gridSend.CurrentRow.Cell["ID"].value);
因?yàn)樵谧鰣?bào)表,還需要將GridView打印成報(bào)表。這里我用的是GridReport工具,這里邊提供了豐富的功能??梢灾苯幼龀霈F(xiàn)成的報(bào)表,在頁(yè)面通過GridReport.start()顯示,也可以通過編碼將GridView中的值傳如報(bào)表中。還是介紹后一種方法:
private void ReportFetchRecord() { for (int row = 0; row < gridSend.RowCount;++row ) { Report.DetailGrid.Recordset.Append(); //for循環(huán)中ColumnCount-1!如果沒有-1,會(huì)拋出 for (int col = 0; col < gridSend.ColumnCount-1; ++col) Report.DetailGrid.Recordset.Fields[col + 1].AsString = gridSend.Rows[r ow].Cells[col].Value.ToString(); Report.DetailGrid.Recordset.Post(); } } private void DefineReport() { Report.Clear();//清空?qǐng)?bào)表 Report.Printer.PaperOrientation = GRPaperOrientation.grpoLandscape; //定義表頭 IGRReportHeader Reportheader = Report.InsertReportHeader(); Reportheader.Height = 1.38; //插入一個(gè)靜態(tài)文本框,顯示報(bào)表標(biāo)題文字 IGRStaticBox StaticBox = Reportheader.Controls.Add(GRControlType.grctStaticBox).AsStaticBox; StaticBox.Text = "發(fā)放物品清單"; StaticBox.Font.Point = 15; StaticBox.Font.Bold = true; StaticBox.Top = 0.40; StaticBox.Width = 5.64; StaticBox.Height = 0.58; //根據(jù)Gridview的列信息定義明細(xì)網(wǎng)絡(luò) Report.InsertDetailGrid(); Report.DetailGrid.ColumnTitle.Height = 0.58; Report.DetailGrid.ColumnContent.Height = 0.58; //將數(shù)據(jù)寫入報(bào)表中 IGRRecordset RecordSet = Report.DetailGrid.Recordset; for (int i = 0; i < gridSend.ColumnCount;++i ) { string ColumnName = gridSend.Columns[i].Name; RecordSet.AddField(ColumnName,GRFieldType.grftString); double ReportColumnWidth = Convert.ToDouble(gridSend.Columns[i].Width)/50; Report.DetailGrid.AddColumn(ColumnName,gridSend.Columns[i].HeaderText,ColumnName,ReportColumnWidth); } } 窗體的構(gòu)造函數(shù)中添加: Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord); 報(bào)表的打印預(yù)覽功能:Report.PrintPreview(true);
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。