我得XML有两个表,如ID是1、2、3、4顺序的使用没问题,但如果改成ID=1、2、4、5时那么在通过RowFilter过滤所得的数据就出问题了,1、2的正常显示4是调用5的数据5没有显示。我通过调试,RowFilter传递的参数没有问题。我得RowFilter的使用方法如下
GridView gvChild = (GridView)e.Row.FindControl("gvChild");
Label lblID = (Label)e.Row.FindControl("lblID");
//lblID是主表ID的绑定值
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("db_kh.xml"));
DataView vd = new DataView(ds.Tables[0]);
vd.RowFilter = "ID='" + lblID.Text + "'";
gvChild.DataSource = vd;
gvChild.DataBind();
上面这段调用第一个表的方法,什么情况下都没有问题都很正常。
下面这段代码在调用表二时就出现了,这个问题:“如ID是1、2、3、4顺序的使用没问题,但如果改成ID=1、2、4、5时那么在通过RowFilter过滤所得的数据就出问题了,1、2的正常显示4是调用5的数据5没有显示。我通过调试,RowFilter传递的参数没有问题。”
protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gvlink = (GridView)e.Row.FindControl("gvlink");
Label lblID = (Label)e.Row.FindControl("lblID");
string id=gvParent.DataKeys[e.Row.ID].Value;
DataSet dl = new DataSet();
dl.ReadXml(Server.MapPath("db_kh.xml")); DataView vdl = new DataView(dl.Tables[1]);
vdl.RowFilter = "User_id='" + lblID.Text + "'";
gvlink.DataSource = vdl;
gvlink.DataBind();
}
}XML文件如下:
这样时就没有问题
<?xml version="1.0" encoding="utf-8"?>
<Users>
<User ID="0">
<Name>朗力</Name>
<Email>[email protected]</Email>
<Title>测试</Title>
<Content>初始数据</Content>
<Date>2007-8-10</Date>
<Reply Name="Biology" Content="回复初始数据" />
</User>
<User ID="1">
<Name>啊强</Name>
<Email>[email protected]</Email>
<Title>我在测试一下看行不行,</Title>
<Content>我 的刀痕活很好的</Content>
<Date>2007-8-30 0:00:00</Date>
<Reply Name="我 " Content="永永远远也因" />
</User>
<User ID="2">
<Name>王强</Name>
<Email>[email protected]</Email>
<Title>三等分三等分三等分 </Title>
<Content>三等分三等分三等</Content>
<Date>2007-8-31 0:00:00</Date>
<Reply Name="88888" Content="1111" />
<Reply Name="88888" Content="1111" />
</User>
</Users>
如果这样就出问题了
<?xml version="1.0" encoding="utf-8"?>
<Users>
<User ID="0">
<Name>朗力</Name>
<Email>[email protected]</Email>
<Title>测试</Title>
<Content>初始数据</Content>
<Date>2007-8-10</Date>
<Reply Name="Biology" Content="回复初始数据" />
</User>
<User ID="2">
<Name>啊强</Name>
<Email>[email protected]</Email>
<Title>我在测试一下看行不行,</Title>
<Content>我 的刀痕活很好的</Content>
<Date>2007-8-30 0:00:00</Date>
<Reply Name="我 " Content="永永远远也因" />
</User>
<User ID="3">
<Name>王强</Name>
<Email>[email protected]</Email>
<Title>三等分三等分三等分 </Title>
<Content>三等分三等分三等</Content>
<Date>2007-8-31 0:00:00</Date>
<Reply Name="88888" Content="1111" />
<Reply Name="88888" Content="1111" />
</User>
</Users>
GridView gvChild = (GridView)e.Row.FindControl("gvChild");
Label lblID = (Label)e.Row.FindControl("lblID");
//lblID是主表ID的绑定值
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("db_kh.xml"));
DataView vd = new DataView(ds.Tables[0]);
vd.RowFilter = "ID='" + lblID.Text + "'";
gvChild.DataSource = vd;
gvChild.DataBind();
上面这段调用第一个表的方法,什么情况下都没有问题都很正常。
下面这段代码在调用表二时就出现了,这个问题:“如ID是1、2、3、4顺序的使用没问题,但如果改成ID=1、2、4、5时那么在通过RowFilter过滤所得的数据就出问题了,1、2的正常显示4是调用5的数据5没有显示。我通过调试,RowFilter传递的参数没有问题。”
protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gvlink = (GridView)e.Row.FindControl("gvlink");
Label lblID = (Label)e.Row.FindControl("lblID");
string id=gvParent.DataKeys[e.Row.ID].Value;
DataSet dl = new DataSet();
dl.ReadXml(Server.MapPath("db_kh.xml")); DataView vdl = new DataView(dl.Tables[1]);
vdl.RowFilter = "User_id='" + lblID.Text + "'";
gvlink.DataSource = vdl;
gvlink.DataBind();
}
}XML文件如下:
这样时就没有问题
<?xml version="1.0" encoding="utf-8"?>
<Users>
<User ID="0">
<Name>朗力</Name>
<Email>[email protected]</Email>
<Title>测试</Title>
<Content>初始数据</Content>
<Date>2007-8-10</Date>
<Reply Name="Biology" Content="回复初始数据" />
</User>
<User ID="1">
<Name>啊强</Name>
<Email>[email protected]</Email>
<Title>我在测试一下看行不行,</Title>
<Content>我 的刀痕活很好的</Content>
<Date>2007-8-30 0:00:00</Date>
<Reply Name="我 " Content="永永远远也因" />
</User>
<User ID="2">
<Name>王强</Name>
<Email>[email protected]</Email>
<Title>三等分三等分三等分 </Title>
<Content>三等分三等分三等</Content>
<Date>2007-8-31 0:00:00</Date>
<Reply Name="88888" Content="1111" />
<Reply Name="88888" Content="1111" />
</User>
</Users>
如果这样就出问题了
<?xml version="1.0" encoding="utf-8"?>
<Users>
<User ID="0">
<Name>朗力</Name>
<Email>[email protected]</Email>
<Title>测试</Title>
<Content>初始数据</Content>
<Date>2007-8-10</Date>
<Reply Name="Biology" Content="回复初始数据" />
</User>
<User ID="2">
<Name>啊强</Name>
<Email>[email protected]</Email>
<Title>我在测试一下看行不行,</Title>
<Content>我 的刀痕活很好的</Content>
<Date>2007-8-30 0:00:00</Date>
<Reply Name="我 " Content="永永远远也因" />
</User>
<User ID="3">
<Name>王强</Name>
<Email>[email protected]</Email>
<Title>三等分三等分三等分 </Title>
<Content>三等分三等分三等</Content>
<Date>2007-8-31 0:00:00</Date>
<Reply Name="88888" Content="1111" />
<Reply Name="88888" Content="1111" />
</User>
</Users>
dl.ReadXml(Server.MapPath("db_kh.xml"));DataView vdl = new DataView(dl.Tables[1]);
vdl.RowFilter = "User_id='" + lblID.Text + "'";
gvlink.DataSource = vdl;
gvlink.DataBind();
------------------------
请问用dataset读取你上面的id会有User_id这个字段吗?
而且dl.Tables[1]也不是user,而是Reply,你要看清楚xml的结构,试试下面的:
DataSet dl = new DataSet();
dl.ReadXml(Server.MapPath("db_kh.xml"));DataView vdl = new DataView(dl.Tables[0]);
vdl.RowFilter = "id='" + lblID.Text + "'";
gvlink.DataSource = vdl;
gvlink.DataBind();
而且dl.Tables[1]也不是user,而是Reply,你要看清楚xml的结构,”这个字段是我在调试过程中发现的,它会将第一个user表的ID,在第二Reply个表中命名为User_id,没问题的,只要ID号顺序一样就一点问题都没有,只要ID的顺序一断就不行了,不知道为何。谢谢
DataSet dl = new DataSet();
dl.ReadXml(Server.MapPath("db_kh.xml"));DataView vdl = new DataView(dl.Tables[0]);
vdl.RowFilter = "id='" + lblID.Text + "'";
gvlink.DataSource = vdl;
gvlink.DataBind();"
执行这段代码就达不到我的要求了,这样只是在user中过滤,没法调用Reply表的内容了
------------------
那就看你的lblID.Text是不是跟xml文件中id一样咯,你的lblID.Text是怎么赋值,是不是连续赋值的
原来User_ID的值是系统自己顺序给加的,我的vdl.RowFilter是按照<User ID="0">去过滤的所以出现问题,现在我改成e.Row.RowIndex方式来过滤就没问题了具体代码如下:
public string idss;//全局变量
protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
idss=Convert.ToString(e.Row.RowIndex);// 将父grideview的RowIndex赋值给idss
GridView gvChild = (GridView)e.Row.FindControl("gvChild");
Label lblID = (Label)e.Row.FindControl("lblID"); //DataView vd = new DataView(ds.Tables[1]);
//vd.RowFilter = "User_id='"+lblID.Text+"'"; DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("db_kh.xml"));
DataView vd = new DataView(ds.Tables[0]);
vd.RowFilter = "ID='" + lblID.Text + "'";
//string sql = "select * from PageLink where PID=" + lblID.Text;
//DataSet ds = Iwoak.ExecSQL.ExecuteDataset(sql);
gvChild.DataSource = vd;//ds.Tables[0].DefaultView;
//gvChild.RowDataBound += new GridViewRowEventHandler(gvChild_RowDataBound);
gvChild.DataBind();
}
}
protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Attributes.Add("style", "word-break :break-all ; word-wrap:break-word");
GridView gvlink = (GridView)e.Row.FindControl("gvlink");
DataSet dl = new DataSet();
dl.ReadXml(Server.MapPath("db_kh.xml"));
DataView vdl = new DataView(dl.Tables[1]);
vdl.RowFilter = "User_id='" + idss + "'";
gvlink.DataSource = vdl; // ds.Tables[0].DefaultView;
gvlink.DataBind();
}
}