GridView控件的使用-創(chuàng)新互聯

GridView控件,用表格的形式將數據顯示出來。那么如何將將數據綁定到GridView中呢?目前值學習了兩種,一種是直接綁定數據源,另一種是動態(tài)顯示數據(主要使用對象有SqlCommand,SqlDataAdapter,DateSet)。這里主要說一下后一種方法:

創(chuàng)新互聯建站專注于慶元企業(yè)網站建設,成都響應式網站建設,商城建設。慶元網站建設公司,為慶元等地區(qū)提供建站服務。全流程按需策劃設計,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯建站專業(yè)和態(tài)度為您提供的服務

 private void showData(String sql)
        {
            try
            {
                //自動生成表頭
                this.gridSend.AutoGenerateColumns = true;
                //這個屬性是當你點擊GridView中任意一個單元格時,默認選中該單元格所在行
                this.gridSend.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                //簡單的數據庫連接
                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);
            }
        }

顯示出數據后,通過CellClick()事件,獲取選中行的數據,例如獲取選中行中的id值:

int id = Convert.ToInt16(gridSend.CurrentRow.Cell["ID"].value);

因為在做報表,還需要將GridView打印成報表。這里我用的是GridReport工具,這里邊提供了豐富的功能??梢灾苯幼龀霈F成的報表,在頁面通過GridReport.start()顯示,也可以通過編碼將GridView中的值傳如報表中。還是介紹后一種方法:

private void ReportFetchRecord() {
            for (int row = 0; row < gridSend.RowCount;++row )
            {
                Report.DetailGrid.Recordset.Append();
                //for循環(huán)中ColumnCount-1!如果沒有-1,會拋出
                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();//清空報表 
            Report.Printer.PaperOrientation = GRPaperOrientation.grpoLandscape;

            //定義表頭
            IGRReportHeader Reportheader = Report.InsertReportHeader();
            Reportheader.Height = 1.38;
            //插入一個靜態(tài)文本框,顯示報表標題文字
            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;

            //根據Gridview的列信息定義明細網絡
            Report.InsertDetailGrid();
            Report.DetailGrid.ColumnTitle.Height = 0.58;
            Report.DetailGrid.ColumnContent.Height = 0.58;
            //將數據寫入報表中
            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);
            }
            
        }      
 窗體的構造函數中添加:
 Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);  
 報表的打印預覽功能:Report.PrintPreview(true);

另外有需要云服務器可以了解下創(chuàng)新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

分享文章:GridView控件的使用-創(chuàng)新互聯
網頁網址:http://muchs.cn/article48/dheeep.html

成都網站建設公司_創(chuàng)新互聯,為您提供品牌網站設計云服務器、網站建設自適應網站、營銷型網站建設、Google

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

外貿網站建設