我想只在OnRowDataBound事件上将GridView行中的单个单元格设置为Gray(如果是读者HasRows)。代码段会更改整个列颜色而不是单个单元格。例如:在图像中我只希望“Jan”旁边的单元格为灰色。
protected void setcolor(object sender, GridViewRowEventArgs e )
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (txtShopBranch.Text == "Area1")
{
int index = e.Row.RowIndex;
string checkdayone = "SELECT one FROM tblregulardays WHERE months = 'Jan' AND shopbranch = 'Area1' AND one = '1'";
NpgsqlCommand findDayOne = new NpgsqlCommand (checkdayone, con);
con.Open();
NpgsqlDataReader reader = findDayOne.ExecuteReader();
if(reader.HasRows)
{
e.Row.Cells[32].BackColor = System.Drawing.Color.Gray;
}
else
{
e.Row.Cells[32].BackColor = System.Drawing.Color.White;
}
con.Close();
}
}
}
您对每一行使用相同的静态SQL查询:
string checkdayone = @"SELECT one FROM tblregulardays
WHERE months = 'Jan'
AND shopbranch = 'Area1'
AND one = '1'";
所以当然会产生相同的结果。您必须使用参数化查询。也许:
string month = e.Row.Cells[31].Text.Trim();
string checkdayone = @"SELECT one FROM tblregulardays
WHERE months = @Month
AND shopbranch = 'Area1'
AND one = '1'";
NpgsqlCommand findDayOne = new NpgsqlCommand (checkdayone, con);
findDayOne.Parameters.AddWithValue("@Month", month);