#######GridView嵌套棘手问题####### 高手帮忙~~~~~~~
我现在做一个GridView嵌套的东西--(图见附件)
绑定外面的GridView1很容易 直接写一个绑定方法一调用就可以了
但是里面嵌套的GridView2需要通过得到外面GridView1的主键值做为sql语句的where条件(我想我说的能让你明白)
protected void Page_Load(object sender, EventArgs e)
{
Page.SmartNavigation = true;
if (!IsPostBack)
{
Bind();
}
}
#region//公用方法
public void conString()
{
string connstr = System.Configuration.ConfigurationSettings.AppSettings["HotelConString"];
con = new SqlConnection(connstr);
}
#endregion
#region//页面加载时绑定
public void Bind()
{
conString();
string sql = "select * from NullRoomInfo where i_reserve_del = 1";
da = new SqlDataAdapter(sql,con);
ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
okEnable();
DRoomBind();
RoomTypeBind();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Cells[5].FindControl("GridView2").Visible = false;
}
}
#endregion
#region//页面加载时绑定,如果房间数大于1则显示"+",否则显示"-"
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button delDel = (Button)e.Row.Cells[8].FindControl("delBtn");
//添加GridView1的删除按钮提示
delDel.Attributes.Add("onclick", "javascript:return confirm('是否删除订房人信息,请注意,如果删除,这个人订的所有房间都会被删除!')");
//加号时变成减号,减号时变成加号
Label lblnum = (Label)e.Row.Cells[5].FindControl("lblrc");
ImageButton img = (ImageButton)e.Row.Cells[5].FindControl("imgSign");
if (Convert.ToInt32(lblnum.Text) >= 1)
{
img.ImageUrl = @"\Hotel\style\img\add.gif";
}
//*****************************************************************************
//绑定嵌套的GridView2
GridView gvin = (GridView)e.Row.Cells[5].FindControl("GridView2");
int idin = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value.ToString());
string sqlin = "select * from hotel_DroomInfo where i_reserve_id = '" + idin + "'";
conString();
da = new SqlDataAdapter(sqlin, con);
ds = new DataSet();
da.Fill(ds);
gvin.DataSource = ds;
gvin.DataBind();
}
}
#endregion
现在这样已经把GridView1和GridView2都已经绑定好了
但是后面要做一个GridView2(里面被嵌套的GridView)的模板列删除操作
删除后不能像GridView1一样调用自己的Bind()方法,只能通过进入GridView2_RowDataBound()事件进行绑定,这样删除GridView2中数据后页面才回重新绑定(我想做个绑定操作的朋友都应该能理解我的一样),
现在我已经把GridView2_RowDataBound()事件中的代码写好了:
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[3].Attributes.Add("onclick", "javascript:return confirm('是否删除订房人信息?')");
GridView Gv2 = (GridView)e.Row.Parent.Parent;
GridViewRow gvr = (Gri