贴上代码:
aspx:部分
<asp:UpdatePanel ID="uid" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<div >
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="228px" AllowPaging="true" PageSize="2"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdatabound="GridView1_RowDataBound" onrowdeleting="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
onrowcommand="GridView1_RowCommand"
onpageindexchanging="GridView1_PageIndexChanging">
<Columns>
<asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" />
<asp:BoundField DataField="au_lname" FooterText="aaaa" HeaderText="au_lname" />
<asp:BoundField DataField="au_lname" HeaderText="au_lname" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
<br />
</div>
</ContentTemplate>
</asp:UpdatePanel>
.cs部分
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Calendar1.Style["position"] = "absolute";
this.TextBox1.DataBind();
GridViewBind();
}
}
private void GridViewBind()
{
string connStr =ConfigurationManager.AppSettings["DSN"];
SqlConnection conn = new SqlConnection(connStr);
string sql1 = "select top 1 au_id,au_lname,au_fname from authors ";
SqlDataAdapter myAdapter = new SqlDataAdapter(sql1, conn);
DataSet ds = new DataSet();
myAdapter.Fill(ds, "authors");
foreach (DataRow iVer in ds.Tables[0].Rows)
{ string a = iVer[1].ToString();
string b = iVer[2].ToString();
}这里读出来的结果是对的 但是显示的不对
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind(); //数据绑定
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridViewBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//string id = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
string id = GridView1.Rows[e.RowIndex].Cells[0].ToString();
//string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
//string connStr = ConfigurationSettings.AppSettings["DSN"];