C# 如何计算asp gridview中每行1的%并显示在模板字段标签内?
我有一个包含8列的gridview: 用户|%| a | b | c | d | e | f JJJJ |%| 1 | 0 | 0 | 1 | 1 | 1 AAA |%| 1 | 1 | 1 | 1 | 1 | 0C# 如何计算asp gridview中每行1的%并显示在模板字段标签内?,c#,sql,asp.net,gridview,C#,Sql,Asp.net,Gridview,我有一个包含8列的gridview: 用户|%| a | b | c | d | e | f JJJJ |%| 1 | 0 | 0 | 1 | 1 | 1 AAA |%| 1 | 1 | 1 | 1 | 1 | 0 <asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" AutoGenerateColumns="false" runat="server" OnRowDataBound="GridView1_RowDataBo
<asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" AutoGenerateColumns="false"
runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="text" runat="server" Text="%"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="myLabel" Text="%" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="a" HeaderText="a" >
</asp:BoundField>
<asp:BoundField DataField="b" HeaderText="b">
</asp:BoundField>
<asp:BoundField DataField="c" HeaderText="c" >
</asp:BoundField>
<asp:BoundField DataField="d" HeaderText="d" >
</asp:BoundField>
<asp:BoundField DataField="e" HeaderText="e" >
</asp:BoundField>
<asp:BoundField DataField="f" HeaderText="f" >
</asp:BoundField>
</Columns>
</asp:GridView>
关于如何处理这个问题有什么想法吗 当然,列数是固定的,所以必须进行交叉乘法: 6列=100% 4列=x% 为了做到这一点,您可以使用迭代器添加并知道寄存器中1的数量。 诸如此类:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int countTrues = 0;
//i = 1 because your first column is dispensable to this.
for (int i = 1; i < e.Row.Cells.Count; i++)
{
if (Convert.ToBoolean(e.Row.Cells[i].Text))
{
//If you get 1 from the database, it will convert to true, so it will increment the counter.
countTrues++;
}
}
//The amount of "trues" multiplied by 100 divided by 6 which is the number of columns you have.
((Label)e.Row.FindControl("mylabel")).Text = ((countTrues * 100) / 6).ToString();
}
}
然后你会发现:
希望这有帮助。您可以循环RowDataBound事件中的所有单元格,检查数据源中的1值并计算百分比
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the row back to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
decimal NoOfOnes = 0;
decimal NoOfColumns = e.Row.Cells.Count - 1;
//loop all the columns and find the 1's
for (int i = 0; i < NoOfColumns; i++)
{
if (row[i].ToString() == "1")
{
NoOfOnes++;
}
}
//find the label in the current row
Label lbl = e.Row.FindControl("myLabel") as Label;
//display results
lbl.Text = string.Format("{0:N2}", (NoOfOnes / NoOfColumns) * 100);
}
}
我得到以下错误。如果Convert.ToBooleane.Row.Cells[i].Text,则此行上的字符串未被识别为有效的布尔值。这是因为在gridview中,我有一个case语句使真值显示为1吗?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the row back to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
decimal NoOfOnes = 0;
decimal NoOfColumns = e.Row.Cells.Count - 1;
//loop all the columns and find the 1's
for (int i = 0; i < NoOfColumns; i++)
{
if (row[i].ToString() == "1")
{
NoOfOnes++;
}
}
//find the label in the current row
Label lbl = e.Row.FindControl("myLabel") as Label;
//display results
lbl.Text = string.Format("{0:N2}", (NoOfOnes / NoOfColumns) * 100);
}
}