我正在使用gridview在按钮上显示使用c#。
在asp.net中显示 DataSet dsnew = new businessLogic.Biz().getData();
if (dsnew != null && dsnew.Tables[0].Rows.Count > 0)
{
DataView myDataView = new DataView();
myDataView = dsnew.Tables[0].DefaultView;
grdDetail.DataSource = myDataView;
grdDetail.DataBind();
}
并在rowDataBound上,根据条件,我想更改dataitem容器值。我是按照以下方式进行的,但问题是所有行都有数据集中最后一行的paymentmethod。
<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();
}
}
答案 0 :(得分:0)
而不是使用 rowDataBound event 来检查要显示的值,您可以尝试直接控制可见性或直接设置文本属性你的专栏!我为你做了一个小例子,可以很容易地适应你的需要!
<强> 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>
<强>代码隐藏强>
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();
}
CodeBehind显示演示值
// 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();
}
}
应该导致:。