【问题标题】:How to bind gridview with data by using loop?如何使用循环将gridview与数据绑定?
【发布时间】:2015-12-16 07:15:01
【问题描述】:

我已经编写了如下代码行的代码

   protected void grdView_DataBinding(object sender, EventArgs e)
    {
        LicenceBL lbl = new LicenceBL(0);
        DataSet lds = new DataSet();
        lbl.FetchForEdit(lds, LicenseType);
        foreach (GridViewRow row in grdView.Rows)
        {
            Label lblJurisdiction = row.FindControl("lblJurisdiction") as Label;
            TextBox txtDateIssued = row.FindControl("txtEffectiveDate") as TextBox;
            TextBox txtDateExpiration = row.FindControl("txtExpirationDate") as TextBox;
            TextBox txtLicenseNumber = row.FindControl("txtLicenseNumber") as TextBox;
            for (int i = 0; i < lds.Tables[0].Rows.Count; i++)
            {
                txtLicenseNumber.Text = lds.Tables[0].Rows[i]["LicenceNumber"].ToString();
            }
        }


    }

我想在不使用gridview 的datasource 属性的情况下绑定grid view。上面的代码不起作用...

假设 lds 包含类似的数据

================================================ ======

LicenceNumber - LicenseIssueDate

123 - 2014 年 12 月 10 日

345 - 2013 年 12 月 1 日

================================================ ======

同样Grid也会包含数据

================================================ ======

LicenceNumber - LicenseIssueDate

123 - 2014 年 12 月 10 日

345 - 2013 年 12 月 1 日

================================================ ======

这是网格视图的设计

                        <asp:GridView ID="grdView" AutoGenerateColumns="false" OnDataBinding="grdView_DataBinding" BorderWidth="0" runat="server" CssClass="table">
                            <Columns>
                            <asp:TemplateField HeaderText="License Number">
                                   <ItemTemplate>
                                      <asp:TextBox ID="txtLicenseNumber" style="padding:12px 5px;" runat="server" />
                                       <br />
                                        <asp:RequiredFieldValidator ID="ValReqLN" Display="Dynamic" runat="server"
                                    ErrorMessage="License Number cannot be Blank." ControlToValidate="txtEffectiveDate"></asp:RequiredFieldValidator>
                                   </ItemTemplate>
                               </asp:TemplateField>

                                    <asp:TemplateField HeaderText="Effective Date">
                                   <ItemTemplate>
                                    <asp:TextBox ID="txtEffectiveDate" style="padding:12px 5px;" placeholder="(mm/dd/yyyy)" CssClass="datepiker" runat="server"></asp:TextBox>
                 </ItemTemplate>
                 </asp:TemplateField>
  </Columns>

                        </asp:GridView>

请帮忙!!!

【问题讨论】:

  • 你的输出应该是什么样的?现在它应该输出最后一行的“LicenseNumber”字段。
  • 我想以编程方式绑定gridview,因为我必须根据业务逻辑在gridview的行内给出许多条件才能在gridview中显示数据。这里我想通过数据集lds绑定gridview。
  • 这不能回答我的问题。您可以模拟输出并将屏幕截图粘贴到您的问题吗?
  • “不工作”取决于您的期望。你有错误吗?您正在尝试做什么,请在您的问题中添加,让其他人帮助您;)参考:stackoverflow.com/help/mcve
  • 基本上我想通过使用循环来绑定gridview。我们通常使用像 GridView1,DataSource = lds 这样的 DataSource 来绑定网格视图,但在这里我想以编程方式绑定网格视图......请帮助我!!!

标签: c# asp.net


【解决方案1】:

为什么不使用datasource 属性?你说:“我想绑定 gridview programically 因为我必须根据业务逻辑在 gridview 的行中给出许多条件才能在 gridview 中显示数据。

您可以这样做:

背后的代码:

protected void grdView_DataBinding(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
   {
    DataRowView drv = e.Row.DataItem as DataRowView;
    if (drv["txtLicenseNumber"].ToString().ToLower() == "abc")
    {
       //Apply business logic here on each row, hide/show etc
        e.Row.CssClass = "highlighted";
    }
   }    
}

在此处阅读更多信息:

Dynamically change GridView Cell value using RowDataBound event in ASP.Net using C# and VB.Net

Selectively apply css to a row in a gridview

【讨论】:

  • 这句话会出现在这个声明中吗?如果 (drv["txtLicenseNumber"].ToString().ToLower() == "ABC") 。我认为您的意思是 .ToUpper() 或 "abc" ?
猜你喜欢
  • 2013-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-17
  • 2017-12-16
  • 1970-01-01
相关资源
最近更新 更多