<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicGridView.aspx.cs" Inherits="DynamicGridView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false">
     
        </asp:GridView>
        <asp:ListBox ID="ListBox1" runat="server" Height="91px" Width="69px">
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
            <asp:ListItem>4</asp:ListItem>
        </asp:ListBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
    </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DynamicGridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["num"] = getNumber();
        }

        if (ListBox1.SelectedValue !="")
        {

            BoundField bf=null, bf1=null, bf2=null, bf3=null, bf4=null;

            GridView1.Columns.Clear();
          
            switch (Int16.Parse (ListBox1.SelectedValue))
            {
                case 1:
                    bf = new BoundField();
                    bf.DataField = "job_id";
                    bf.HeaderText = "job_id";
                    bf.FooterText = Session["num"].ToString();
                   
                    GridView1.Columns.Add(bf);
                   
                    break;
                case 2:
                    bf1 = new BoundField();
                    bf1.DataField = "job_id";
                    bf1.HeaderText = "job_id";
                    bf1.FooterText = Session["num"].ToString();
                    GridView1.Columns.Add(bf1);
                    bf2 = new BoundField();
                    bf2.DataField = "job_desc";
                    bf2.HeaderText = "job_desc";
                    bf2.FooterText = Session["num"].ToString();
                    GridView1.Columns.Add(bf2);
                    break;
                case 3:
                    bf1 = new BoundField();
                    bf1.DataField = "job_id";
                    bf1.HeaderText = "job_id";
                    bf1.FooterText = Session["num"].ToString();
                    GridView1.Columns.Add(bf1);
                    bf2 = new BoundField();
                    bf2.DataField = "job_desc";
                    bf2.HeaderText = "job_desc";
                    bf2.FooterText = Session["num"].ToString();
                    GridView1.Columns.Add(bf2);
                    bf3 = new BoundField();
                    bf3.DataField = "max_lvl";
                    bf3.HeaderText = "max_lvl";
                    bf3.FooterText = Session["num"].ToString();
                    GridView1.Columns.Add(bf3);
                    break;
                case 4:
                    bf1 = new BoundField();
                    bf1.DataField = "job_id";
                    bf1.HeaderText = "job_id";
                    bf1.FooterText = Session["num"].ToString();
                    GridView1.Columns.Add(bf1);
                    bf2 = new BoundField();
                    bf2.DataField = "job_desc";
                    bf2.HeaderText = "job_desc";
                    bf2.FooterText = Session["num"].ToString();
                    GridView1.Columns.Add(bf2);
                    bf3 = new BoundField();
                    bf3.DataField = "max_lvl";
                    bf3.HeaderText = "max_lvl";
                    bf3.FooterText = Session["num"].ToString();
                    GridView1.Columns.Add(bf3);
                    bf4 = new BoundField();
                    bf4.DataField = "min_lvl";
                    bf4.HeaderText = "min_lvl";
                    bf4.FooterText = Session["num"].ToString();
                    GridView1.Columns.Add(bf4);
                    break;
            }
            BindGridView();
        }

    }
    int getNumber()
    {
        string connect = "Data Source=localhost\\sqlexpress;Integrated Security=True;Initial Catalog=pubs";
        System.Data.SqlClient.SqlConnection sqlconnect = new System.Data.SqlClient.SqlConnection(connect);
        string command = "select * from jobs";
        System.Data.SqlClient.SqlCommand sqlcommand = new System.Data.SqlClient.SqlCommand();
        sqlcommand.CommandText = command;
        sqlcommand.Connection = sqlconnect;

        System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
        da.SelectCommand = sqlcommand;
        DataSet ds = new DataSet();

        sqlconnect.Open();
        sqlcommand.ExecuteNonQuery();
        da.Fill(ds);

        int num = ds.Tables[0].Rows.Count;

        sqlconnect.Close();
        return num;


    }
    void BindGridView()
    {
        string connect = "Data Source=localhost\\sqlexpress;Integrated Security=True;Initial Catalog=pubs";
        System.Data.SqlClient.SqlConnection sqlconnect = new System.Data.SqlClient.SqlConnection(connect);
        string command = "select * from jobs";
        System.Data.SqlClient.SqlCommand sqlcommand = new System.Data.SqlClient.SqlCommand();
        sqlcommand.CommandText = command;
        sqlcommand.Connection = sqlconnect;

        System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
        da.SelectCommand = sqlcommand;
        DataSet ds = new DataSet();

        sqlconnect.Open();
        sqlcommand.ExecuteNonQuery();
        da.Fill(ds);

        int num = ds.Tables[0].Rows.Count;

        GridView1.ShowFooter = true;
             
        GridView1.DataSource = ds;
        GridView1.DataBind();

        sqlconnect.Close();

    }
   
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}
//增加按钮
grdSubject.AutoGenerateColumns = false;
        grdSubject.ShowHeader = true;
        grdSubject.DataKeyNames = new String[] { "Id" };
        DataControlFieldCollection dcfc = grdSubject.Columns;
        dcfc.Clear();
ButtonField selectRow = new ButtonField(); selectRow.ButtonType = ButtonType.Button; selectRow.CommandName = "Select"; selectRow.Text = "选择"; selectRow.HeaderText = "A"; dcfc.Add(selectRow); ButtonField delRow = new ButtonField(); delRow.ButtonType = ButtonType.Button; delRow.AccessibleHeaderText = "Delete"; delRow.CommandName = "Delete"; delRow.Text = "删除"; delRow.CausesValidation = true; dcfc.Add(delRow);
protected void grdSubject_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //判断是否是DataRow
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标经过Row时的效果
            e.Row.Attributes.Add("onmouseover", "e=this.style.backgroundColor; this.style.backgroundColor='linen'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=e");

            //当开始时间大于现在时间,显示行为蓝色
            if (DateTime.Parse(e.Row.Cells[2].Text) > DateTime.Now)
            {
                e.Row.BackColor = Color.LightSkyBlue;
            }
            //当结束时间小于现在时间,显示行为灰色
            if (DateTime.Parse(e.Row.Cells[3].Text) < DateTime.Now)
            {
                e.Row.BackColor = Color.Silver;
            }

            //当点击删除按钮时激活提示
            Button btn = (Button)e.Row.Cells[5].Controls[0];
            btn.Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
        }
    }

    protected void grdSubject_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }

    protected void grdSubject_RowCommand(object sender, EventArgs e)
    {
        ////单击Grid中按钮时发生throw new Exception("The method or operation is not implemented.");
        //int selIndex = Convert.ToInt32(e.CommandArgument);
        //GridViewRow selectedRow = grdSubject.Rows[selIndex];

        //if (e.CommandName == "Select")
        //{
        //    //txtSubjectTitle.Text = selectedRow.Cells[1].Text;
        //    //dateBegin.Value = selectedRow.Cells[2].Text;
        //   // dateEnd.Value = selectedRow.Cells[3].Text;

        //}
        //if (e.CommandName == "Delete")
        //{
        //    //Hsf.Touch.Dto.Subject item = new Hsf.Touch.Dto.Subject();
        //    //item.Id = int.Parse(grdSubject.DataKeys[selIndex].Value.ToString());
        //    try
        //    {
        //        //TouchFactory.CreateSubjectManage().Delete(item);
        //        //删除成功,清除输入框内容
        //        //Clear();
        //    }
        //    catch (Exception err)
        //    {
        //        //ShowMessageBox("删除失败 \n" + err.Message);
        //    }
        //}

        //ShowGrid();
    }
    protected void grdSubject_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }

 

posted on 2012-10-24 11:48  Black Bean  阅读(1110)  评论(0编辑  收藏  举报