dataGrid1.Select(1);
dataGrid1.SelectionBackColor = Color.Red;
这样的话.0行跟1行都会变成红色的
想了好一会儿只想到下面的方法:
private void button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=student;Integrated Security=True");
SqlDataAdapter daAuthors = new SqlDataAdapter("Select * From users where A<60", cn);
cn.Open();
daAuthors.Fill(ds, "student");
dataGrid1.DataSource = ds.Tables[0];
cn.Close();
int datagridnumber = ds.Tables[0].Rows.Count;//获取dataGrid1的总行数
for (int i = 0; i < datagridnumber; i++)
{
MessageBox.Show(i.ToString ());//i就是A<60的行数
}
}
兄弟可以把上面的方法写成函数,然后调用一下,就能得到行数了,
private void button5_Click(object sender, EventArgs e)
{
string sql = path.Text;//连接数据库
string sele = "SELECT * from GYS ";
OleDbConnection conn = new OleDbConnection(sql);
OleDbDataAdapter dataa = new OleDbDataAdapter(sele, conn);
DataSet dataset1 = new DataSet();
conn.Open();
dataa.Fill(dataset1, "GYS");
conn.Close();
DataGridCell Mycell = new DataGridCell();
string b = "";
Mycell.ColumnNumber = 5;//字段所在列(这个列数是固定的所以可以算出)
for (int a=1;a<Convert.ToInt32((dataset1.Tables["GYS"].Rows.Count.ToString()));a++)
{
Mycell.RowNumber = a;
b = this.dataGrid1[Mycell].ToString();
if (Convert.ToInt32(b) < 60)
dataGrid1.Select(a);
}
dataGrid1.SelectionBackColor = Color.Red;
}