两张表:
A表
PId PName
10 湖北
11 上海
12 浙江
13 北京B表
CID CName PId
1 上海 11
2 武汉 10
3 北京 13
4 南昌 12接下来我把GridView(绑定表B)和DropDownList(绑定表A)分别绑定,不在编辑状态时,用的Lable能正常显示成
1 上海 上海可一点编辑按钮 DropDownList 显示结果是 如下:
1 上海 湖北而我希望在编辑状态下显示的结果是
1 上海 上海
======================================================================================================================================================
有哪为高手帮忙解决一下这问题.
A表
PId PName
10 湖北
11 上海
12 浙江
13 北京B表
CID CName PId
1 上海 11
2 武汉 10
3 北京 13
4 南昌 12接下来我把GridView(绑定表B)和DropDownList(绑定表A)分别绑定,不在编辑状态时,用的Lable能正常显示成
1 上海 上海可一点编辑按钮 DropDownList 显示结果是 如下:
1 上海 湖北而我希望在编辑状态下显示的结果是
1 上海 上海
======================================================================================================================================================
有哪为高手帮忙解决一下这问题.
是不是AJAX?
还是会POSTBACK?
如果你要POSTBACK的话,那你只能再绑定一次了~~
贴代码看下~
是不是AJAX?
还是会POSTBACK?
如果你要POSTBACK的话,那你只能再绑定一次了~~
贴代码看下~======================================================
DropDownList 没有设置postback,没用AJAX
两个表没外键关联,我只是通过SQL语句把两个表关联起来了.
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" AllowPaging="True"
PageSize="5" DataKeyNames="L_ID" CellPadding="4" ForeColor="#333333" GridLines="None"
OnPageIndexChanging="gv_PageIndexChanging" OnRowCancelingEdit="gv_RowCancelingEdit"
OnRowDeleting="gv_RowDeleting" OnRowEditing="gv_RowEditing" OnRowUpdating="gv_RowUpdating" OnRowDataBound="gv_RowDataBound">
<Columns>
<asp:BoundField DataField="L_ID" HeaderText="ID" ReadOnly="True" />
<asp:BoundField DataField="L_Name" HeaderText="Name" />
<asp:TemplateField HeaderText="DDL/Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("W_Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlName" runat="server" DataSource='<%# BindDdl() %>' DataValueField="W_ID"
DataTextField="W_Name" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>后台代码:
private SqlConnection Con()
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShangHai"].ToString());
con.Open(); return con;
}
private DataTable tableL()
{
DataTable dt = new DataTable(); try
{
SqlDataAdapter sda = new SqlDataAdapter("SELECT L.*,(SELECT W.W_Name FROM LW_TableW W WHERE W.W_ID=L.L_Number ) AS W_Name FROM LW_Table AS L", Con());
sda.Fill(dt);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
} return dt;
}
private void BindGv()
{
this.gv.DataSource = tableL();
this.gv.DataBind();
}
public SqlDataReader BindDdl()
{
SqlCommand com = new SqlCommand("SELECT * FROM LW_TableW", Con());
SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection); return dr;
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
gv.EditIndex = e.NewEditIndex; strShow = ((Label)gv.Rows[e.NewEditIndex].Cells[2].Controls[1]).Text; //Response.Write(strShow); BindGv();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
你可以用DropDownList.Text=B表的字段
编辑时可能需要重指定value
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) == 0)
{
//保存当前行的au_id的值
string W_ID = this.gv.DataKeys[e.Row.RowIndex]["W_ID"].Value.ToString();
DropDownList ddlName = (DropDownList)e.Row.FindControl("ddlName"); if (ddlName != null)
{
ddlName.DataSource = BindDdl()
ddlName.DataTextField = "W_ID";
ddlName.DataValueField = "W_ID";
ddlName.DataBind(); ListItem item = ddlName.Items.FindByText(W_ID);
if (item != null)
{
item.Selected = true;
}
}
} }
}
<EditItemTemplate>
<asp:DropDownList ID="ddlName" runat="server" DataSource='<%# BindDdl() %>' DataValueField="W_ID"
DataTextField="W_Name" >
</asp:DropDownList>
</EditItemTemplate>
当在编辑状态的时,DropDownList的数据源是绑定的A表(因为我显示的是B表里PId对应A表PId所查询出来的PName),而此时DropDownList显示的只是按A表查询出来的结果显示,并没有按原先B表里的PId来首先显示对应的PName!
这点需要解决
问一下你代码里的
if ((e.Row.RowState & DataControlRowState.Edit) == 0)
这是什么意思?
而且我很奇怪,在没编辑状态下的行 ,e.Row.RowState显示的要么是普通行,要么显示的是交替行,但都能到if(){...//可以走到这里执行} 里面去执行代码,可一但到了正在编辑的行,就直接跳过
if ((e.Row.RowState & DataControlRowState.Edit) == 0){//此时编辑的行进不来}的判断??
第一个比较完整的例子:(中间的Ddl所绑定的ID已经不是从1开始了)
protected void gvCity_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) != 0)
{
e.Row.Cells[0].BackColor = System.Drawing.Color.Red; DropDownList ddl = ((DropDownList)e.Row.Cells[3].FindControl("DDLP")); if (ddl != null)
{
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(lblPName));
}
}
}
}第个例子:(DDL所绑定的ID是从1到10)
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Response.Write(e.Row.RowState+"<br>"); if ((e.Row.RowState & DataControlRowState.Edit) != 0)
{
e.Row.Cells[0].BackColor = System.Drawing.Color.Red;//处在编辑状态的行的第一列背景色是红色 DropDownList ddlW = (DropDownList)e.Row.FindControl("ddlName");
if (ddlW != null)
{ //因为strShow得到的值是从1到10,数据库里的ID自增,并且从1到10,
//这样才能用Convert.ToInt32(strShow)-1,如果ID自增之后,
//其中ID变为 1..10,12,30这样不规则的,那DDL所绑定的ID就不可能
//与数据库里ID一致,这时在用Convert.ToInt32(strShow)-1就已经
//超出了DDL的索引范围.
//这时还有一个办法,就是循环出DDL.Item.Count,然后一一比较在编辑
//状态之前Lable里的值,如果对应就做为DDL在正在编辑时的首选值
ddlW.Items[Convert.ToInt32(strShow)-1].Selected = true;
}
else
{
Response.Write("没找到DDL");
}
}
}
}结帖