在gridview
内玩数据时,我真的很糟糕。这里我有一个简单的gridview
,其中包含一个dropdownlist
,它从数据库表Products
中获取数据。
我想要的是下拉列表OnSelectedIndexChanged
,价格标签应该在下拉列表中读取所选产品的价格。问题是当我选择dropdownlist
中的产品时价格没有显示。 label
仍为空。
ASP.NET
<asp:GridView ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false" PagerStyle-CssClass="pager" HeaderStyle-CssClass="header" RowStyle-CssClass="rows" AllowPaging="true" PageSize="5" OnRowDataBound="Gridview1_RowDataBound">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="#">
<HeaderStyle CssClass="header" Width="60px"></HeaderStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:DropDownList ID="dropdown1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropdown1_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle CssClass="header" />
<ItemStyle Width="170px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="QTY">
<ItemTemplate>
<asp:TextBox ID="qty_txtbox" runat="server" style="text-align:center;" OnTextChanged="TextBox2_TextChanged"></asp:TextBox>
</ItemTemplate>
<ControlStyle Width="50px" CssClass="txt" />
<HeaderStyle CssClass="header" />
<ItemStyle Width="50px" CssClass="txt" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Price (AED)">
<ItemTemplate>
<asp:Label ID="amount_lbl" runat="server"></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="header" />
<ItemStyle Width="130px" CssClass="txt" />
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:ImageButton runat="server" ID="trash" Style="height: 20px;" ImageUrl="~/IMG/garbage.png" />
</ItemTemplate>
<ControlStyle Height="20px" Width="20px"></ControlStyle>
<FooterStyle HorizontalAlign="center" />
<HeaderStyle Height="30px" Width="30px" CssClass="header"></HeaderStyle>
<FooterTemplate>
<asp:ImageButton runat="server" ID="addnew" ImageUrl="~/IMG/add.png" Style="height: 20px;" OnClick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="header" />
<PagerStyle CssClass="pagerr" />
<RowStyle CssClass="rows" />
</asp:GridView>
以下是我尝试的内容
private DataSet GetData()
{
SqlCommand cmd = new SqlCommand("SELECT ProductName, PartNumber, price FROM Products");
using (SqlConnection con = new SqlConnection(cDate.CS))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddproducts = (e.Row.FindControl("dropdown1") as DropDownList);
ddproducts.DataSource = GetData();
ddproducts.DataTextField = "ProductName";
ddproducts.DataValueField = "ProductName";
ddproducts.DataBind();
//Add Default Item in the DropDownList
ddproducts.Items.Insert(0, new ListItem("<----Please select---->"));
}
}
protected void dropdown1_SelectedIndexChanged(object sender, EventArgs e)
{
string dvalue = Gridview1.SelectedRow.Cells[1].Text;
string price = Gridview1.SelectedRow.Cells[3].Text;
using (SqlConnection con = new SqlConnection(cDate.CS))
{
con.Open();
SqlCommand myCommand = new SqlCommand("select price from products where ProductName = @name");
myCommand.Parameters.AddWithValue("@name", dvalue);
myCommand.Connection = con;
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
while (myReader.Read())
{
price = (myReader["price"].ToString());
}
}
}
}
错误
此行Object reference not set to an instance of an object.
的 string dvalue = Gridview1.SelectedRow.Cells[1].Text;
答案 0 :(得分:2)
您似乎没有将价格设定回电网!
更新:正如“非幸运”所提到的,为了获得适当的网格行,我们使用了触发事件的下拉列表并在其父项中获得关联DataRow
:
protected void dropdown1_SelectedIndexChanged(object sender, EventArgs e)
{
var row = (sender as DropDownList).NamingContainer as GridViewRow; //instead of Gridview1.SelectedRow;
string dvalue = row.Cells[1].Text; //or row.FindControl(id);
//string price = row.Cells[3].Text;
string price = "1400"; //get from database
row.Cells[3].Text = price; //or row.FindControl(id);
//Gridview1.new
}
答案 1 :(得分:2)
正如另一个答案所述,问题的主要问题是您没有更新该特定标签的结果。但这只能解决您的问题,您还可以做更多的事情:
整个过程可以使用以下代码实现,如果您需要任何说明,请查看并告诉我们:
protected void dropdown1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlProduct = (DropDownList)sender;
DataGridItem row = (DataGridItem) ddlProduct.NamingContainer;
Label lblPrice = (Label)row.FindControl("amount_lbl");
// Get current label Text
string price = lblPrice.Text;
// Perform your operations here
// Assign the price value back to the label
lblPrice.Text = price;
}