case_detail表
现在我有了一个带有dropdownlist的gridview。 基本上,gridview显示来自case_detail表的数据,而下拉列表显示来自callout_status表的数据。
当前,我可以从数据库中填充下拉列表。 对于ItemTemplate,它可以显示从数据库中选择的值。但是我无法在Edittemplate中显示选定的值,每当我按下编辑按钮时,db中的选定选项将不会显示。任何想法都该怎么做。非常感谢。
aspx页面
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="id"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" BackColor="White"
BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3"
CellSpacing="1" GridLines="None" OnRowDataBound="gvItem_RowDataBound" >
<Columns>
<asp:TemplateField HeaderText="收方">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" ></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("receiver_status_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="寄方">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" ></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("shipper_status_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="case_id">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("case_id") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("case_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remarks">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("remark") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("remark") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
</div>
</form>
aspx.cs页面
protected void Page_Load(object sender, EventArgs e)
{
string connString = "Server=localhost;Database=cs_case;Uid=root;Pwd='';SslMode=none";
MySqlConnection con = new MySqlConnection(connString);
if (!Page.IsPostBack)
{
refreshdata();
}
}
public void refreshdata()
{
string connString = "Server=localhost;Database=cs_case;Uid=root;Pwd='';SslMode=none";
MySqlConnection con = new MySqlConnection(connString);
MySqlCommand cmd = new MySqlCommand("select id, remark,case_id,shipper_status_id,receiver_status_id from case_detail", con);
MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
refreshdata();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string connString = "Server=localhost;Database=cs_case;Uid=root;Pwd='';SslMode=none";
MySqlConnection con = new MySqlConnection(connString);
int id = Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());
MySqlCommand cmd = new MySqlCommand("delete from case_detail where id = @id", con);
cmd.Parameters.AddWithValue("@id", id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
refreshdata();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
refreshdata();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string connString = "Server=localhost;Database=cs_case;Uid=root;Pwd='';SslMode=none";
MySqlConnection con = new MySqlConnection(connString);
int id = Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());
TextBox txtname = GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox;
TextBox txtemail = GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox;
DropDownList drpgender = GridView1.Rows[e.RowIndex].FindControl("DropDownList2") as DropDownList;
DropDownList drpgender2 = GridView1.Rows[e.RowIndex].FindControl("DropDownList3") as DropDownList;
MySqlCommand cmd = new MySqlCommand("update case_detail set remark=@remark, case_id=@case_id,shipper_status_id=@shipper_status_id,receiver_status_id=@receiver_status_id where id =@id", con);
cmd.Parameters.AddWithValue("@remark", txtname.Text);
cmd.Parameters.AddWithValue("@case_id", txtemail.Text);
cmd.Parameters.AddWithValue("@shipper_status_id", drpgender.SelectedItem.Text);
cmd.Parameters.AddWithValue("@receiver_status_id", drpgender2.SelectedItem.Text);
cmd.Parameters.AddWithValue("@id", id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
refreshdata();
GridView1.EditIndex = -1;
refreshdata();
}
//dropdown
protected void gvItem_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//1st dopdown
var DropDownList1 = e.Row.FindControl("DropDownList2") as DropDownList;
if (DropDownList1 != null)
{
var dt = new DataTable();
using (var con = new MySqlConnection("Server=localhost;Database=cs_case;Uid=root;Pwd='';SslMode=none"))
{
con.Open();
// var cmd = new MySqlCommand("Select * from callout_status where category ='shipper'", con);
var cmd = new MySqlCommand("Select * from callout_status where category ='shipper'", con);
var da = new MySqlDataAdapter(cmd);
da.Fill(dt);
}
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "status";
DropDownList1.DataValueField = "status";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("", "NA"));
}
//2nd dopdown
var DropDownList3 = e.Row.FindControl("DropDownList3") as DropDownList;
if (DropDownList3 != null)
{
var dt = new DataTable();
using (var con = new MySqlConnection("Server=localhost;Database=cs_case;Uid=root;Pwd='';SslMode=none"))
{
con.Open();
var cmd = new MySqlCommand("Select * from callout_status where category ='receiver'", con);
var da = new MySqlDataAdapter(cmd);
da.Fill(dt);
}
DropDownList3.DataSource = dt;
DropDownList3.DataTextField = "status";
DropDownList3.DataValueField = "status";
DropDownList3.DataBind();
var value = e.Row.DataItem.ToString();
DataRowView dr = e.Row.DataItem as DataRowView;
DropDownList3.SelectedValue = value; // you can use e.R
DropDownList3.Items.Insert(0, new ListItem("", "NA"));
}
}
}
答案 0 :(得分:0)
您需要在gvItem_RowDataBound
中检查该行是否处于编辑模式。
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
//find the items in the edit row
var DropDownList3 = e.Row.FindControl("DropDownList3") as DropDownList;
DropDownList3.DataSource = dt;
DropDownList3.DataTextField = "status";
DropDownList3.DataValueField = "status";
DropDownList3.DataBind();
}
else
{
//every other row
var Label2 = e.Row.FindControl("Label2") as Label;
Label2.Text = "Label 2 found";
}
}