当前位置:编程学习 > C#/ASP.NET >>

GridView如何实现把左边的三列固定

如图所示,向右滑动滚动条时,序号,日期和周固定。:
 
                <asp:Panel runat="server" ID="panel">
                    <asp:GridView ID="gvDestination" runat="server"
                        OnRowDataBound="gvDestination_RowDataBound" EnableViewState="False" 
                        Width="100%" CellPadding="4" ForeColor="#333333" GridLines="None">
                        <AlternatingRowStyle BackColor="White" />
                        <EmptyDataTemplate>
                            <span>没有数据</span>
                        </EmptyDataTemplate>
                        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                        <SortedAscendingCellStyle BackColor="#FDF5AC" />
                        <SortedAscendingHeaderStyle BackColor="#4D0000" />
                        <SortedDescendingCellStyle BackColor="#FCF6C0" />
                        <SortedDescendingHeaderStyle BackColor="#820000" />
                    </asp:GridView>
                    </asp:Panel>
                </div>  



private DataTable GetBindData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("日期");
        dt.Columns.Add("周");
        DataSet dsUser = Userinfo_BLL.selectPersonInfo();
        if (dsUser != null && dsUser.Tables.Count > 0 && dsUser.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in dsUser.Tables[0].Rows)
            {
                dt.Columns.Add(dr["Name"].ToString());
            }
        }
        string  targetDateStr = System.Configuration.ConfigurationManager.ConnectionStrings["StartDate"].ConnectionString;
        DateTime targetDate;
        if (!DateTime.TryParse(targetDateStr, out targetDate))
            targetDate = DateTime.Today;
        while (targetDate <= DateTime.Today.AddDays(7))
        {
            DataRow drNew = dt.NewRow();
            drNew["日期"] = targetDate.ToString("yyyy-MM-dd");
            drNew["周"] = GetWeekFromDate(targetDate);
            dt.Rows.InsertAt(drNew,0);
            targetDate = targetDate.AddDays(1);
        }
        DataSet dsDestination = PersonDestinationBll.selectPersontination();
        if (dsDestination != null && dsDestination.Tables.Count > 0 && dsDestination.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in dsDestination.Tables[0].Rows)
            {
                foreach (DataRow drNew in dt.Rows)
                {
                    if ((Convert.ToDateTime(dr["Date"]) - Convert.ToDateTime(drNew["日期"])).Days == 0)
                    {
                        drNew[dr["Name"].ToString()] = dr["Destination"];
                        break;
                    }
                }
            }
        }
        return dt;
    }

    private string GetWeekFromDate(DateTime dt)
    {
        string str = string.Empty;
        switch (dt.DayOfWeek)
        {
            case DayOfWeek.Monday:
                str = "星期一";
                break;
            case DayOfWeek.Tuesday:
                str = "星期二";
                break;
            case DayOfWeek.Wednesday:
                str = "星期三";
                break;
            case DayOfWeek.Thursday:
                str = "星期四";
                break;
            case DayOfWeek.Friday:
                str = "星期五";
                break;
            case DayOfWeek.Saturday:
                str = "星期六";
                break;
            case DayOfWeek.Sunday:
                str = "星期日";
                break;
            default:
                break;
        }
        return str;
    }

    protected void gvDestination_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Text = "<nobr>" + e.Row.Cells[0].Text + "</nobr>";
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataTable dt = gvDestination.DataSource as DataTable;
            int cellNums = e.Row.Cells.Count;
            for (int i = 2; i < cellNums; i++)
            {
                DropDownList ddl = new DropDownList();
                ddl.DataTextField = "Name";
                ddl.DataValueField = "SpecialData";
                ddl.DataSource = GetDestState(e.Row.Cells[1].Text, e.Row.Cells[2].Text, dt.Columns[i].Caption);
                ddl.DataBind();
                ddl.SelectedValue = string.Format("{0},{1},{2},{3}", e.Row.Cells[i].Text, e.Row.Cells[1].Text, e.Row.Cells[2].Text, dt.Columns[i].Caption);
                System.Web.UI.HtmlControls.HtmlGenericControl span1 = new System.Web.UI.HtmlControls.HtmlGenericControl("span");
                span1.Attributes.Add("style", "width:78px; height:24px;display:block;");
                System.Web.UI.HtmlControls.HtmlGenericControl span2 = new System.Web.UI.HtmlControls.HtmlGenericControl("span");
                span2.Attributes.Add("style", "display:block; width:56px; overflow:hidden;");
                ddl.Attributes.Add("style", "width:80px; height:24px; background:none; border:none;");
                ddl.Attributes.Add("onchange", "update(this.options[this.options.selectedIndex].value)");
                span1.Controls.Add(span2);
                span2.Controls.Add(ddl);
                e.Row.Cells[i].Controls.Add(span1);
            }
            e.Row.Cells[0].Text = "<nobr>" + e.Row.Cells[0].Text + "</nobr>";
            e.Row.Cells[1].Text = "<nobr>" + e.Row.Cells[1].Text + "</nobr>";
            e.Row.Cells[2].Text = "<nobr>" + e.Row.Cells[2].Text + "</nobr>";
        }
    }
GridView 固定显示 --------------------编程问答-------------------- http://download.csdn.net/detail/liuchaolin/5325853 --------------------编程问答-------------------- 很有用,多谢 --------------------编程问答-------------------- 你有实现了,我现在也是需要这个功能。能共享吗?
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,