zoukankan      html  css  js  c++  java
  • GridView绑定列

    最近做了个GridView的数据绑定,在数据库中读取一条记录包含产品名字,植入日期,植入量,要做如下显示:

    如此数据源就不好绑定,只好把列数做成固定的,绑定最近7天数据,当然此时列标题还是要最时间变化而变化的,代码如下:

    <asp:GridView RowStyle-Wrap="false" runat="server" ShowHeader="true" AutoGenerateColumns="False"
    ID="gvData" Width="98%" GridLines="Both" BorderWidth="1px" EnableModelValidation="false"
    CellPadding="4" AutoGenerateEditButton="false">
    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    <Columns>
    <asp:TemplateField HeaderText="植入量ID" Visible="false">
    <ItemTemplate>
    <asp:Label ID="lblImplantQuantityID" runat="server" Text='<%# Eval("lepu_implantedquantityId") %>'
    Visible="false"></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="产品ID" Visible="false">
    <ItemTemplate>
    <asp:Label ID="lblProductId" runat="server" Text='<%# Eval("lepu_ThirdCatalogId") %>'
    Visible="false"></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="产品名字">
    <ItemTemplate>
    <asp:Label ID="lblProductName" runat="server" Text='<%# Eval("productName") %>' Width="200px"></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:TextBox runat="server" ID="txtImplantQuantity1" Text='<%#Bind("lepu_implantedquantity1") %>' Width="80px"></asp:TextBox></ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:TextBox runat="server" ID="txtImplantQuantity2" Text='<%#Bind("lepu_implantedquantity2") %>' Width="80px"></asp:TextBox></ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:TextBox runat="server" ID="txtImplantQuantity3" Text='<%#Bind("lepu_implantedquantity3") %>' Width="80px"></asp:TextBox></ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:TextBox runat="server" ID="txtImplantQuantity4" Text='<%#Bind("lepu_implantedquantity4") %>' Width="80px"></asp:TextBox></ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:TextBox runat="server" ID="txtImplantQuantity5" Text='<%#Bind("lepu_implantedquantity5") %>' Width="80px"></asp:TextBox></ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:TextBox runat="server" ID="txtImplantQuantity6" Text='<%#Bind("lepu_implantedquantity6") %>' Width="80px"></asp:TextBox></ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:TextBox runat="server" ID="txtImplantQuantity7" Text='<%#Bind("lepu_implantedquantity7") %>' Width="80px"></asp:TextBox></ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <EmptyDataTemplate>
    暂无数据</EmptyDataTemplate>
    <EmptyDataRowStyle BackColor="#E5E5E5" ForeColor="black" HorizontalAlign="Center"
    Font-Bold="True" />
    <RowStyle Wrap="False" HorizontalAlign="Center" VerticalAlign="Middle"></RowStyle>
    <PagerStyle BackColor="#d22238" ForeColor="White" HorizontalAlign="Center" Font-Bold="True" />
    <HeaderStyle BackColor="#E5E5E5" Font-Bold="True" />
    <AlternatingRowStyle BackColor="White" />
    </asp:GridView>

    后台代码:

    private void ChangeDataSourceType(List<wcfService.ImplantedQuantity> implantedQuantity)
    {
    Dictionary<string, string[]> implantQuantityList = new Dictionary<string, string[]>();
    for (int index = 0; index < implantedQuantity.Count; index++)
    {
    if (!implantQuantityList.ContainsKey(implantedQuantity[index].lepu_ThirdCatalogId))
    {
    string[] quantity = new string[9];//第八个数组中存放产品名,第九个数据存放植入量ID,其他对应存放植入量
    implantQuantityList.Add(implantedQuantity[index].lepu_ThirdCatalogId, quantity);
    quantity[7] = implantedQuantity[index].productName;
    quantity[8] = implantedQuantity[index].lepu_implantedquantityId;
    }
    int at = (DateTime.Now.Date - DateTime.Parse(implantedQuantity[index].lepu_implanteddate).ToLocalTime().Date).Days;
    implantQuantityList[implantedQuantity[index].lepu_ThirdCatalogId][at] = implantedQuantity[index].lepu_implantedquantity;
    }
    DataBindGridView(implantQuantityList);
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="implantQuantityList"></param>
    protected void DataBindGridView(Dictionary<string, string[]> implantQuantityList)
    {
    DataTable dt = new DataTable();
    dt.Columns.Add("lepu_ThirdCatalogId");
    dt.Columns.Add("lepu_implantedquantityId");
    dt.Columns.Add("productName");
    dt.Columns.Add("lepu_implantedquantity1");
    dt.Columns.Add("lepu_implantedquantity2");
    dt.Columns.Add("lepu_implantedquantity3");
    dt.Columns.Add("lepu_implantedquantity4");
    dt.Columns.Add("lepu_implantedquantity5");
    dt.Columns.Add("lepu_implantedquantity6");
    dt.Columns.Add("lepu_implantedquantity7");
    foreach (var item in implantQuantityList)
    {
    dt.Rows.Add(item.Key, item.Value[8], item.Value[7], item.Value[6], item.Value[5], item.Value[4], item.Value[3], item.Value[2], item.Value[1], item.Value[0]);
    }

    gvData.DataSource = dt;
    for (int i = 3; i < gvData.Columns.Count; i++)//一定要在列存在后进行列名赋值,也就是要在列内容绑定后绑定列名,否则不存在列就不会绑定到任何数据
    {
    gvData.Columns[i].HeaderText = DateTime.Today.AddDays(i - 9).ToShortDateString();

    //此时在设计页面不能有<HeaderTemplate>

    //<asp:Label runat="server" ID="lblImplantDate4" Text='<%#Eval("lepu_implanteddate4") %>'></asp:Label></HeaderTemplate>

    }
    gvData.DataBind();//在绑定列标题时,一定要记者添加该句话,否则在页面上不会显示列标题
    }

    protected List<wcfService.ImplantedQAdjust> GetDataFromGridView()
    {
    List<wcfService.ImplantedQAdjust> implantQAdjustList = new List<wcfService.ImplantedQAdjust>();
    for (int i = 0; i < gvData.Rows.Count; i++)
    {
    for (int j = 3; j < gvData.Columns.Count; j++)
    {
    wcfService.ImplantedQAdjust impantQAdujust = new wcfService.ImplantedQAdjust();
    GridViewRow gRow = gvData.Rows[i];
    impantQAdujust.lepu_reportdate = DateTime.Now.ToString();
    impantQAdujust.lepu_dealer = dllOutCompany.SelectedValue;
    impantQAdujust.lepu_hospital = dllHospital.SelectedValue;
    impantQAdujust.lepu_implantedquantityid = ((Label)gRow.FindControl("lblImplantQuantityID")).Text;
    impantQAdujust.lepu_ThirdCatalogId = ((Label)gRow.FindControl("lblProductId")).Text;
    string implantQuantity = "txtImplantQuantity" + (j - 2);
    impantQAdujust.lepu_adjustmentisimplanted = ((TextBox)gRow.FindControl(implantQuantity)).Text;
    impantQAdujust.lepu_implanteddate = gvData.Columns[j].HeaderText;
    implantQAdjustList.Add(impantQAdujust);
    }
    }
    return implantQAdjustList;
    }

  • 相关阅读:
    SQL Server, Timeout expired.all pooled connections were in use and max pool size was reached
    javascript 事件调用顺序
    Best Practices for Speeding Up Your Web Site
    C语言程序设计 使用VC6绿色版
    破解SQL Prompt 3.9的几步操作
    Master page Path (MasterPage 路径)
    几个小型数据库的比较
    CSS+DIV 完美实现垂直居中的方法
    由Response.Redirect引发的"Thread was being aborted. "异常的处理方法
    Adsutil.vbs 在脚本攻击中的妙用
  • 原文地址:https://www.cnblogs.com/GreenGrass/p/2678732.html
Copyright © 2011-2022 走看看