Reputation: 243
I have a gridview with 8 columns:
user|%|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_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>
The a-f columns have bit data type pulled from SQL database (True and False). I added a template field with an asp Label to display the % column that I would like to get the percentage of 1's for each row but can't figure out how to calculate this and display in the label. So I want to calculate the percentage of times the value 1 shows up per row and display it in that rows label.
I can get to the label by this:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
(Label)e.Row.FindControl("mylabel") = (The Percentage of 1's counted by row);
}
}
Any ideas on how to approach this?
Upvotes: 0
Views: 355
Reputation: 35514
You can loop all the cells in the RowDataBound event and check the data source for 1
values and calculate the percentage.
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);
}
}
Upvotes: 1
Reputation: 527
Well, of course, the number of columns is fixed, so you have to make cross multiplication:
6 columns = 100%
4 columns = x%
In order to do that, you can use an iterator to add and know the number of 1's in your register.
Something like that:
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();
}
}
Hope this helps.
Upvotes: 0