有人可以帮我解决这个问题吗?我有一个返回 nmumeric 值的网格视图。第一列始终是类别,并且总是有十一个。出于说明目的,我只显示了三个 columsa 和两行。第一个数字是我拥有的,第二个是我需要的。
Event type | JAN-01 | Feb-01 | Mar-01
---------------------------------------
| Item 1 | 21 | 100 | 0
---------------------------------------
| Item 2 | 5 | 1 | 67
---------------------------------------
Event type | TOTAL
---------------------
| Item 1 | 121 |
---------------------
| Item 2 | 73 |
---------------------
我正在玩的代码是这样的。网格视图标记:
<td>
<asp:GridView ID="gvSystemRMRiskRpt" runat="server" AutoGenerateColumns="true" CellPadding="3"
PageSize="25" BackColor="White" BorderColor="MidnightBlue" BorderStyle="Groove"
BorderWidth="1px" CssClass="TextCompact" GridLines="Vertical" OnRowDataBound="gvSystemRMRiskRpt_OnDataBound"
EmptyDataText="Your request has returned zero records">
<EmptyDataRowStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="Gainsboro" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" VerticalAlign="Top" />
</asp:GridView>
</td>
然后是代码文件:
protected void btnSearch_Click(object sender, EventArgs e)
{
Guid site = new Guid(ddlSites.SelectedValue);
int interval = Convert.ToInt16(ddlIntervals.SelectedValue);
string[] startDate = tbStartDate.Text.Split('/');
string[] stopDate = tbEndDate.Text.Split('/');
DataTable _dt = RMRiskData.dtESRByMonthYear(connectionManager, site, startDate[1], stopDate[1], interval);
gvSystemRMRiskRpt.DataSource = _dt;
gvSystemRMRiskRpt.DataBind();
lblSelectedSite.Text = ddlSites.SelectedItem.ToString();
}
protected void gvSystemRMRiskRpt_OnDataBound(object sender, GridViewRowEventArgs e)
{
_totals = 0;
// Formats columns headers
if (e.Row.RowType == DataControlRowType.Header)
{
string[] _dateParse;
string _year = string.Empty;
string _month = string.Empty;
switch (ddlIntervals.SelectedValue.ToString())
{
case "1":
for (int i = 1; i <= e.Row.Cells.Count - 1; i++)
{
_dateParse = e.Row.Cells[i].Text.Split('-');
_year = _dateParse[0].ToString().Remove(0, 2);
_month = getMonthAccronyms(_dateParse[1].ToString());
e.Row.Cells[i].Text = _month + "-" + _year;
e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
_dateParse = null;
}
break;
case "2":
for (int i = 1; i <= e.Row.Cells.Count - 1; i++)
{
_dateParse = e.Row.Cells[i].Text.Split('-');
_year = _dateParse[0].ToString();
e.Row.Cells[i].Text = _year;
}
break;
case "3":
e.Row.Cells[1].Text = "Totals";
break;
default:
for (int i = 1; i <= e.Row.Cells.Count - 1; i++)
{
_dateParse = e.Row.Cells[i].Text.Split('-');
_year = _dateParse[0].ToString().Remove(0, 2);
_month = getMonthAccronyms(_dateParse[1].ToString());
e.Row.Cells[i].Text = _month + "-" + _year;
_dateParse = null;
}
break;
}
}
// Formats mumbers to add comma separators
int _number1 = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (ddlIntervals.SelectedValue.ToString() == "3")
{
CultureInfo ci = new CultureInfo("en-US");
for (int i = 1; i <= e.Row.Cells.Count - 1; i++)
{
if (e.Row.Cells[i].Text.ToString() != " ")
{
_number1 = Convert.ToInt32(e.Row.Cells[i].Text);
_totals = _totals + _number1;
}
e.Row.Cells[i].Text = _totals.ToString("#,##0", ci);
}
}
else
{
CultureInfo ci = new CultureInfo("en-US");
int _number = 0;
for (int i = 1; i <= e.Row.Cells.Count - 1; i++)
{
if (e.Row.Cells[i].Text.ToString() != " ")
{
_number = Convert.ToInt32(e.Row.Cells[i].Text);
e.Row.Cells[i].Text = _number.ToString("#,##0", ci);
}
else
{
e.Row.Cells[i].Text = "0";
}
}
}
}
}
我现在得到的结果是这样的:
Event type | TOTAL| FEB-02
----------------------------
| Item 1 | 121 | 97 |
--------------------- -------
| Item 2 | 73 | 21 |
-----------------------------
我将不胜感激!
谢谢,
理庄