0

I have a WinForms Application that reads in some data from tables in Access using a query and prints it out to a datagridview. I was just wondering if there was any possible way I could indent or highlight a row with a matching "SN". There should be two matching "SN's" but I would only like to indent or highlight one of them. Here's what I have if it's any help...

ad.SelectCommand = new OleDbCommand("SELECT b.RSV_CAT, b.SEQNUM, b.LEASE,  b.WELL_ID, a.QUALIFIER, a.KEYWORD, a.EXPRESSION FROM [AC_ECONOMIC] a INNER JOIN [AC_PROPERTY] b on a.PROPNUM=b.PROPNUM WHERE a.KEYWORD = '" + end + "' AND a.QUALIFIER = '" +qual+ "' AND a.EXPRESSION LIKE 'SN%'", con); 

ad3.SelectCommand = new OleDbCommand("SELECT b.RSV_CAT, b.SEQNUM, b.LEASE,  b.WELL_ID, a.QUALIFIER, a.KEYWORD, a.EXPRESSION FROM [AC_ECONOMIC] a INNER JOIN [AC_PROPERTY] b on a.PROPNUM=b.PROPNUM WHERE a.KEYWORD = '" + start + "' AND a.QUALIFIER = '" + qual + "' AND a.EXPRESSION LIKE 'SN%'", con3);

I'd like to indent or highlight the query with the start variable in it or the ad3.Selectcommand. Any help would be much appreciated.

Also here's some of the dataGridView code I have that gets rid of the borders...

private void newWindow_Load(object sender, EventArgs e)
{            
    this.dataGridView3.AdvancedCellBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None;
    this.dataGridView3.AdvancedCellBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;

    this.dataGridView3.AdvancedCellBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
    this.dataGridView3.AdvancedCellBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;

    dataGridView3.DataSource = ((Form1)frm1).ds.Tables[0];
}
4

2 回答 2

0

好的,这是一个非常简单的Winforms DatagridView 示例,它在满足特定条件时循环单元格并突出显示行。到目前为止,这是您需要的确切解决方案,但可以为您提供指导:-)

//Creation of DataTable
DataTable t = new DataTable("myTable");
t.Columns.Add(new DataColumn("Field1", typeof(string)));
t.Columns.Add(new DataColumn("Field2", typeof(int)));
t.Columns.Add(new DataColumn("Field3", typeof(string)));
t.Columns.Add(new DataColumn("Field4", typeof(string)));

//Adding test data to DataTable
t.Rows.Add(new object[] { "Value1", 300, "Test1", "Test2" });
t.Rows.Add(new object[] { "Value2", 1100, "Test3", "Test4" });
t.Rows.Add(new object[] { "Value3", 900, "Test5", "Test6" });
t.Rows.Add(new object[] { "Value4", 100, "Test7", "Test8" });
t.Rows.Add(new object[] { "Value5", 1200, "Test9", "Test10" });

//Creation of DataSet
DataSet s = new DataSet();
s.Tables.Add(t);

//Assigning DataSet to DataGridView (here's where you start looking)
dataGridView1.DataSource = s;
dataGridView1.DataMember = "myTable";

foreach (DataGridViewRow r in dataGridView1.Rows)
{
    foreach (DataGridViewCell c in r.Cells)
    {
        if (c != null && c.Value != null) //to avoid blank or 'new' rows
        {
            if (c.OwningColumn.Name == "Field2" && (int)c.Value > 1000)
            {
                foreach (DataGridViewCell cell in c.OwningRow.Cells)
                {
                    cell.Style.BackColor = Color.Aquamarine;
                }
            }
        }
    }
}
于 2013-07-24T15:33:25.773 回答
0

我正在使用指南示例,并在最后一部分迭代单元格时使用了不同的方法。(在 VB 中)

    Dim tbl As DataTable = New DataTable
    tbl.Columns.Add("Field1") ' default type of String
    tbl.Columns.Add("Field2", GetType(System.Int32))
    tbl.Columns.Add("Field3")
    tbl.Columns.Add("Field4")
    'Adding test data to DataTable
    tbl.Rows.Add(New Object() {"Value1", 300, "Test1", "Test2"})
    tbl.Rows.Add(New Object() {"Value2", 1100, "Test3", "Test4"})
    tbl.Rows.Add(New Object() {"Value3", 900, "Test5", "Test6"})
    tbl.Rows.Add(New Object() {"Value4", 100, "Test7", "Test8"})
    tbl.Rows.Add(New Object() {"Value5", 1200, "Test9", "Test10"})

    ' alternate way to add and address data, second column left as DbNull
    Dim x As DataRow

    x = tbl.NewRow
    x(0) = "Val x"
    x("Field3") = "test"
    x!Field4 = "xyzzy"
    tbl.Rows.Add(x)

    For i As Int32 = 1 To 5 ' add more data
        x = tbl.NewRow
        x(0) = "Val x"
        x!Field2 = i * 250
        x("Field3") = "test"
        x!Field4 = "xyzzy"
        tbl.Rows.Add(x)
    Next

    'Assigning DataTable to DataGridView DataSource
    DataGridView1.DataSource = tbl
    DataGridView1.ClearSelection()

    For Each r As DataGridViewRow In DataGridView1.Rows
        If Not IsDBNull(r.Cells("field2").Value) AndAlso CInt(r.Cells("Field2").Value) > 1000 Then
            For Each cell As DataGridViewCell In r.Cells
                cell.Style.BackColor = Color.Aquamarine
            Next
        End If
    Next
于 2013-07-24T20:08:33.070 回答