如何使用循环将gridview与数据绑定?

时间:2015-12-16 07:15:02

标签: c# asp.net

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

   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属性的情况下绑定网格视图。上面的代码不起作用......

让我们假设lds包含像

这样的数据

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

LicenceNumber - LicenceIssueDate

123 - 12/10/2014

345 - 12/1/2013

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

类似Grid也将包含数据

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

LicenceNumber - LicenceIssueDate

123 - 12/10/2014

345 - 12/1/2013

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

这是网格视图的设计

                        <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>

请帮忙!!!

1 个答案:

答案 0 :(得分: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