如何按位置显示数据

时间:2013-05-29 18:06:38

标签: c# sql-server gridview

如果不是正确的方法,我不需要使用Gridview,有些人告诉我使用Repeater,但我不知道怎么做。需要帮助!

现在看起来像这样:

enter image description here

我想按位置(sql表中的一列)将其分开:

enter image description here

ASPX

<asp:GridView runat="server" ID="ReportGrid" CssClass="table" AutoGenerateColumns="False"
        AllowPaging="True" BorderColor="#E8CC6B" BorderStyle="Solid" BorderWidth="1px"
        Width="100%" OnRowDataBound="ReportGrid_RowDataBound">
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="Owes" HeaderText="Owes" />
            <asp:BoundField DataField="Paid" HeaderText="Paid" />
            <asp:BoundField DataField="OrigAmt" HeaderText="Amt" />
            <asp:BoundField DataField="SubmitDate" DataFormatString="{0:MM/dd/yy}" HeaderText="Date" />
        </Columns>
    </asp:GridView>

ASPX.CS

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            bindGridView();
    }
    public void bindGridView()
    {
        string connStr = "";
        SqlConnection mySQLconnection = new SqlConnection(connStr);
        if (mySQLconnection.State == ConnectionState.Closed)
        {
            mySQLconnection.Open();
        }
        SqlCommand mySqlCommand = new SqlCommand(@"SELECT CASE Location
                                 WHEN 1 then 'North'
                                 WHEN 2 then 'South'
                                 WHEN 4 then 'East'
                                 WHEN 5 then 'West'
                               end as Locations, Name, Owes, Paid, OrigAmt, SubmitDate FROM Cure ", mySQLconnection);

        SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
        DataSet myDataSet = new DataSet();
        mySqlAdapter.Fill(myDataSet);
        ReportGrid.DataSource = myDataSet;
        ReportGrid.DataBind();
        if (mySQLconnection.State == ConnectionState.Open)
        {
            mySQLconnection.Close();
        }

    }

    protected void ReportGrid_RowDataBound(object sender,
                                                GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[3].ForeColor = System.Drawing.Color.Green;
            e.Row.Cells[2].ForeColor = System.Drawing.Color.Red;

        }

1 个答案:

答案 0 :(得分:0)

尝试向GridView控件添加Locations列,如下所示:

<asp:GridView runat="server" ID="ReportGrid" CssClass="table" AutoGenerateColumns="False"
        AllowPaging="True" BorderColor="#E8CC6B" BorderStyle="Solid" BorderWidth="1px"
        Width="100%" OnRowDataBound="ReportGrid_RowDataBound">
        <Columns>
            <asp:BoundField DataField="Locations" HeaderText="Location" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="Owes" HeaderText="Owes" />
            <asp:BoundField DataField="Paid" HeaderText="Paid" />
            <asp:BoundField DataField="OrigAmt" HeaderText="Amt" />
            <asp:BoundField DataField="SubmitDate" DataFormatString="{0:MM/dd/yy}" HeaderText="Date" />
        </Columns>
</asp:GridView>

如果向GridView添加绑定字段列,则无需使用ASP.NET转发器。希望这会有所帮助。