首页上一页 1 下一页尾页 2 条记录 1/1页
关于《网络编程自学手册》中获取GridView控件中的数据的2个问题?
发表在ASP.NET图书答疑
2009-02-17
是否精华
是
否
版块置顶:
是
否
1,在《范例宝典》实例Ex04_27中,我增加一个GridView控件(GridView2)和一个Button控件,我想按Button控件时,GridView2显示我在GridView1中选择的数据,程序如下:
protected void Button_Click(object sender, EventArgs e)
{
sqlcon = new SqlConnection(strCon);
SqlCommand sqlcom;
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{
string sqlstr = "select * from tb_Member where id='" + GridView1.DataKeys[i].Value + "'";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "tb_Member");
GridView2.DataSource = myds;
GridView2.DataBind();
sqlcon.Close();
}
}
}
为何GridView2控件只显最后一行数据?
2,为获取GridView控件中的数据,我在控件中增加一个CommandField下的" 选择"字段,我根据《网络编程自学手册》ch08_6例子的:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//设置GridView控件的编辑项的索引为选择的当前索引
GridView1.EditIndex = e.NewEditIndex;
Bind();//自定义事件用来绑定GridView
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//设置GridView控件的编辑项的索引为-1,即取消编辑
GridView1.EditIndex = -1;
Bind();//自定义事件用来绑定GridView
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection con = new SqlConnection("server=.;database=mr_08;uid=sa;pwd=;");
con.Open();
//获得学生编号字段
string id=((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.Trim();
TextBox1.Text = id.Tostrng();///这里为何不显示数据???
}
为何在TextBox1.Text不显示我选中行的id字段?
protected void Button_Click(object sender, EventArgs e)
{
sqlcon = new SqlConnection(strCon);
SqlCommand sqlcom;
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{
string sqlstr = "select * from tb_Member where id='" + GridView1.DataKeys[i].Value + "'";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "tb_Member");
GridView2.DataSource = myds;
GridView2.DataBind();
sqlcon.Close();
}
}
}
为何GridView2控件只显最后一行数据?
2,为获取GridView控件中的数据,我在控件中增加一个CommandField下的" 选择"字段,我根据《网络编程自学手册》ch08_6例子的:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//设置GridView控件的编辑项的索引为选择的当前索引
GridView1.EditIndex = e.NewEditIndex;
Bind();//自定义事件用来绑定GridView
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//设置GridView控件的编辑项的索引为-1,即取消编辑
GridView1.EditIndex = -1;
Bind();//自定义事件用来绑定GridView
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection con = new SqlConnection("server=.;database=mr_08;uid=sa;pwd=;");
con.Open();
//获得学生编号字段
string id=((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.Trim();
TextBox1.Text = id.Tostrng();///这里为何不显示数据???
}
为何在TextBox1.Text不显示我选中行的id字段?