C# 更改gridview中Container.DataItem值时发生行数据绑定问题
我正在使用c#在asp.net中使用gridview在按钮上显示 在rowDataBound上,根据条件,我想更改dataitem容器值。我是按照下面的方式做的,但问题是所有行都有数据集中最后一行的paymentmethodC# 更改gridview中Container.DataItem值时发生行数据绑定问题,c#,asp.net,C#,Asp.net,我正在使用c#在asp.net中使用gridview在按钮上显示 在rowDataBound上,根据条件,我想更改dataitem容器值。我是按照下面的方式做的,但问题是所有行都有数据集中最后一行的paymentmethod <asp:TemplateField ItemStyle-Width="70" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Label ID="lblPay
<asp:TemplateField ItemStyle-Width="70" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblPaymentMethod" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"SellingPaymentMethod") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
protected void grdDetail_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
return;
}
foreach (DataRow dtrCurrentRow in (((System.Data.DataView)grdDetail.DataSource)).Table.Rows)
{
//DataRow row = (DataRow)e.Row.DataItem;
Label lblPaymentMethod = e.Row.FindControl("lblPaymentMethod") as Label;
if (Condition1))
{
lblPaymentMethod.Text = dtrCurrentRow["SellingPaymentMethod"].ToString();
}
else if (Condition 2)
{
lblPaymentMethod.Text = dtrCurrentRow["DeliveryPaymentmethod"].ToString();
}
}
受保护的void grdDetail_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
返回;
}
foreach(DataRow dtrCurrentRow in(((System.Data.DataView)grdDetail.DataSource)).Table.Rows)
{
//DataRow row=(DataRow)e.row.DataItem;
标签lblPaymentMethod=e.Row.FindControl(“lblPaymentMethod”)作为标签;
如果(条件1))
{
lblPaymentMethod.Text=dtrCurrentRow[“SellingPaymentMethod”].ToString();
}
否则,如果(条件2)
{
lblPaymentMethod.Text=dtrCurrentRow[“DeliveryPaymentmethod”].ToString();
}
}
您可以尝试直接控制列的可见性或直接设置列的文本属性,而不是使用来检查要显示的值!我为您做了一个小示例,它可以很容易地适应您的需要
ASPX
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Visible='<%# CheckCondition1(Eval("ID")) %>' Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ProductName">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Visible='<%# CheckCondition2(Eval("ProductName")) %>' Text='<%# Bind("ProductName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="thirdColumn">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# GetValue(Eval("ID")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="forthColumn">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# GetValue(Eval("ID"), "staticValue", Eval("ProductName")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码隐藏以显示演示值
// assume there is a class Products with id and ProductName
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<Product> products = new List<Product>();
products.Add(new Product() { ID = 1, ProductName = "asdf" });
products.Add(new Product() { ID = 2, ProductName = "asdf" });
products.Add(new Product() { ID = 3, ProductName = "jklö" });
products.Add(new Product() { ID = 4, ProductName = "asdf" });
gvProducts.DataSource = products;
gvProducts.DataBind();
}
}
//假设存在一个id为且名称为ProductName的类产品
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!Page.IsPostBack)
{
列表产品=新列表();
products.Add(新产品(){ID=1,ProductName=“asdf”});
products.Add(新产品(){ID=2,ProductName=“asdf”});
products.Add(新产品(){ID=3,ProductName=“jklö”});
products.Add(新产品(){ID=4,ProductName=“asdf”});
gvProducts.DataSource=产品;
gvProducts.DataBind();
}
}
应导致以下结果:
public bool CheckCondition1(object ID)
{
return ID.ToString() != "2";
}
public bool CheckCondition2(object ProductName)
{
return ProductName.ToString() != "jklö";
}
public string GetValue(object ID)
{
// check condition here
// ...
// and return according value
return ID.ToString() + " is your ID";
// eg
//if (Condition1))
// return dtrCurrentRow["SellingPaymentMethod"].ToString();
//else if (Condition 2)
// return dtrCurrentRow["DeliveryPaymentmethod"].ToString();
}
public string GetValue(object ID, object firstValue, object secondValue)
{
if (ID.ToString() == "2")
return firstValue.ToString();
else
return secondValue.ToString();
}
// assume there is a class Products with id and ProductName
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<Product> products = new List<Product>();
products.Add(new Product() { ID = 1, ProductName = "asdf" });
products.Add(new Product() { ID = 2, ProductName = "asdf" });
products.Add(new Product() { ID = 3, ProductName = "jklö" });
products.Add(new Product() { ID = 4, ProductName = "asdf" });
gvProducts.DataSource = products;
gvProducts.DataBind();
}
}