我看不出我在哪里做错了。没有数据填充到网格视图中。
这是我的 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();
}
}
希望另一双眼睛可以检测到我的网格视图或后面的代码中的错误。BTW , da,ds, cmd, con 在类中定义。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
查询返回几行,所以那里没有问题。