歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 【辦公采購系統】系統中遇到的問題——GridView

【辦公采購系統】系統中遇到的問題——GridView

日期:2017/3/1 12:06:42   编辑:關於Linux

前言

接上文,【辦公采購系統】系統中遇到的問題匯總(一),已經對一部分知識進行了總結,下面對剩下的問題分析。

Problems

3、GridView

可以說這個控件真是寶貝。非常以及及其的實用。

這裡寫圖片描述

                                                圖 3-1  GridView

GridView控件也有綁定數據的功能,也可以分為三種,基本和上一篇博客中的DropDownList控件一樣,這裡就不做介紹了。

下面小編對他的一下更多的功能展示給大家。

這個基本就是GridView的界面,它有一個小三角,點開就可以看到他的很多的功能,一個一個給大家介紹。

這裡寫圖片描述

                                                圖 3-2  GridView

第一個,自動套用格式

這個功能不用多說,一看便懂。

這裡寫圖片描述

                                                圖 3-3  自動套用格式

第二個,選擇數據源

這個功能基本同於前一篇博客的,【辦公采購系統】系統中遇到的問題匯總(一),功能還是很好的,歡迎大家來訪。

第三個,編輯列

清楚明白,就是編輯表格的列。

這裡寫圖片描述

                                                圖 3-4  自動套用格式

從上一張圖中看到了有這麼多的可選字段,而且還是英語的,下面就為大家一次介紹。

這裡寫圖片描述

                                                圖 3-5  自動套用格式

從上面的表格中可以清楚明白的了解各個字段的類型,也有各個的作用。一般從後台傳來的泛型或者DateTable就可以用BoundField綁定,下面為大家展示:

這裡寫圖片描述

                                                圖 3-6  設置字段標頭文本

這裡還是有一些要強調的,就是要去掉“自動生成字段”,如果不去掉就會形成下圖的樣式。

這裡寫圖片描述

                                                圖 3-7  設置字段標頭文本

第四個,GridView選中,編輯,取消,刪除

在頁面中拖入一個GridView控件;把鼠標移到此控件上,會看到在此控件的右上角有一個小三角,點擊此三角,選擇編輯列,在彈出的對話框中,找到CommandField,展開它,然後會看到要操作那一列,選擇它並點添加,然後取消勾選自動生成字段;

這裡寫圖片描述

                                                圖 3-8  選中,編輯,取消,刪除

後台代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace test
{
    public partial class GridViewTest : System.Web.UI.Page
    {

        //建立數據連接字符串
        SqlConnection cnn = new SqlConnection("server=.;uid=sa;pwd=123456;database=Login");

        #region 頁面加載
        /// 
        /// 頁面加載
        /// 
        ///
        ///
        protected void Page_Load(object sender, EventArgs e)
        {


            GridView1.DataSource = bind();
            GridView1.DataBind();
        }
        #endregion

        #region 分頁頁面改變--重新綁定數據源
        /// 
        /// 分頁頁面改變--重新綁定數據源
        /// 
        ///
        ///
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            bind();
        }
        #endregion 

        #region 刪除
        /// 
        /// 刪除
        /// 
        ///
        ///
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

            SqlCommand sqlcom = new SqlCommand(sqlstr, cnn);
            cnn.Open();
            sqlcom.ExecuteNonQuery();
            cnn.Close();
            bind();
        }
        #endregion 

        #region 修改
        /// 
        /// 修改
        /// 
        ///
        ///
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
        }
        #endregion 

        #region 更新
        /// 
        /// 更新
        /// 
        ///
        ///
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            string sqlstr = "update 表 set 字段1='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
                + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            SqlCommand sqlcom = new SqlCommand(sqlstr, cnn);
            cnn.Open();
            sqlcom.ExecuteNonQuery();
            cnn.Close();
            GridView1.EditIndex = -1;
            bind();
        }
        #endregion

        #region 綁定數據
        /// 
        /// 綁定數據
        /// 
        /// 
        public DataSet bind()
        {

            string SQL = "select * from USERS";
            SqlCommand cmd = new SqlCommand(SQL, cnn);
            cnn.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(SQL, cnn);
            da.Fill(ds);
            cnn.Close();
            return ds;
        }
        #endregion 
    }
}

前台代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewTest.aspx.cs" Inherits="test.GridViewTest" %>



Summarize

Gridview控件真是很強大,他還有很多的功能,在學習過程中,我發現了一個博客: C#精髓 第四講 GridView 72般絕技,這個真的好全,有72個功能!!搞起來吧!!希望對您有幫助!

Copyright © Linux教程網 All Rights Reserved