我的网格视图中没有使用DataSet填充数据

时间:2013-09-20 14:44:48

标签: c# asp.net gridview webforms dataset

我看不出我做错了什么。没有数据填充到网格视图中。

这是我的aspx gridview代码

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="TeamMemberId" 
 Width="772px" CellPadding="4" 
ForeColor="#333333" GridLines="Both" Height="16px" 
    onselectedindexchanged="GridView1_SelectedIndexChanged" >

 <Columns>
 <asp:CommandField ShowSelectButton="True" />
 <asp:BoundField DataField="TeamMemberLanId" HeaderText="LANID"  
     InsertVisible="False" ReadOnly="True" SortExpression="TeamMemberLanId"  />
 <asp:BoundField DataField="First Name" HeaderText="FirstName" 
                                   SortExpression="Name" />
  <asp:BoundField DataField="Last Name" HeaderText="LastName" 
                                   SortExpression="Name" />
   <asp:BoundField DataField="IsActive" HeaderText="Status"  />
 <asp:TemplateField HeaderText="Select">
  <HeaderTemplate>
  <asp:CheckBox ID="chkSelectAll" runat="server" AutoPostBack="true"      OnCheckedChanged="chkSelectAll_CheckedChanged" />
  </HeaderTemplate>
 <ItemTemplate>
  <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true"     OnCheckedChanged="chkSelect_CheckedChanged" />
  </ItemTemplate>
  </asp:TemplateField>

 </Columns>

 <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
 <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
 <PagerStyle BackColor="#FFCC66" ForeColor="#333333" 
                       HorizontalAlign="Center" />
 <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
 <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
 <AlternatingRowStyle BackColor="White" />
 </asp:GridView>

这是我背后的代码。

 public void BindData()
 {


    try
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mystring"].ToString());
        cmd.CommandText = "Select TeamMemberLanId, FirstName, LastName,IsActive from   dbo.LoanOwnerStamp ORDER BY 2,3";
        cmd.Connection = con;
        da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        con.Open();
        cmd.ExecuteNonQuery();
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
        else
        {
            //Bind Empty grdview with Columns names in Header and "No Team Member   record found" message if no records are found in the database
            BindEmptyGridWithHeader(GridView1, ds);
        }
    }
    catch (Exception ex)
    {
        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('" + ex.Message.ToString() + "');", true);
    }
    finally
    {
        con.Close();
    }
 }

希望另一双眼睛能够检测到我的gridview或后面的代码中的错误。 BTW,da,ds,cmd,con在类中定义。

 protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {

        BindData();

    } 
  }

查询返回多行,因此没有问题。

0 个答案:

没有答案