[英]Find a cell of a row in a gridview and do something on OnRowDataBound
I want to only set a single cell in a GridView row to Grey if the reader HasRows, on the OnRowDataBound event. 如果读者在OnRowDataBound事件上具有HasRows,我只想将GridView行中的单个单元格设置为Gray。 The code snippet changes the entire column color rather than a single cell. 该代码段更改了整个列的颜色,而不是单个单元格。 For example: In the image I want only the cell next to "Jan" to be grey. 例如:在图像中,我只希望“ 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();
}
}
}
You use the same static sql query for every row: 您对每一行使用相同的静态sql查询:
string checkdayone = @"SELECT one FROM tblregulardays
WHERE months = 'Jan'
AND shopbranch = 'Area1'
AND one = '1'";
so of course will always yield the same result. 因此,当然总会得到相同的结果。 You have to use a parameterized query. 您必须使用参数化查询。 Maybe: 也许:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.