如果这不是正确的方法,我不需要使用 Gridview,有些人告诉我使用中继器,但我不知道如何。需要帮助!
现在看起来像这样:
我想按位置(sql表中的一列)将其分开:
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;
}