缩进数据网格视图的某些行

时间:2013-07-24 14:57:19

标签: c# ms-access datagridview

我有一个WinForms应用程序,它使用查询从Access中的表中读取一些数据并将其打印到datagridview。我只是想知道是否有任何可能的方法我可以缩进或突出显示匹配“SN”的行。应该有两个匹配的“SN”,但我只想缩进或突出其中一个。如果有任何帮助,这就是我所拥有的......

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);

我想缩进或突出显示其中start变量或ad3.Selectcommand的查询。任何帮助将非常感激。

此外,我还有一些dataGridView代码可以摆脱边界......

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];
}

2 个答案:

答案 0 :(得分: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;
                }
            }
        }
    }
}

答案 1 :(得分: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