如何比较不同datagridview上的每一列?

时间:2015-04-24 15:34:18

标签: c# winforms

假设我有2个datagridview fullrowselect,每个都有两列。例如像这样一个

      DGV1                    DGV2
ControlNum|Title        ControlNum|Title 
0132      |avengers     0112      |X-men
0112      |X-men        0231      |Transformers
0234      |spiderman    0123      |Thor


现在我想将Dvg2的列表与DGV1进行比较,如果DGV2在dgv1中包含相同的数据,那么我想将dgv2中该行的背景单元格变成红色,如果不保持不变的话。 加载db的代码


 void loadDB()//dgv1
    {
        string query = "SELECT animelist.ControlNum,TitleAnime FROM maindatabase.watchlist inner join maindatabase.animelist on watchlist.ControlNum = animelist.ControlNum where idnum=?para;";
        using (MySqlConnection conn = new MySqlConnection(myConnection))
        {                
            conn.Open();
            using (MySqlCommand cmd = new MySqlCommand(query, conn))
            {
                cmd.Parameters.AddWithValue("?para",int.Parse(MyList.AccountControlNum.ToString()));
                try
                {
                    this.Controls.Add(grid);

                    sda = new MySqlDataAdapter();
                    sda.SelectCommand = cmd;
                    datset = new DataTable();
                    grid.DataSource = datset;
                    sda.Fill(datset);
                    bsource = new BindingSource();
                    grid.BackgroundImage = Properties.Resources.Kurumi;
                    grid.SetCellsTransparent();
                    grid.Width = 271;
                    grid.Height = 391;
                    grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    grid.AllowUserToDeleteRows = false;
                    grid.AllowUserToAddRows = false;
                    grid.AllowUserToResizeColumns = false;
                    grid.AllowUserToResizeRows = false;
                    grid.ReadOnly = true;
                    grid.AllowDrop = false;
                    grid.MultiSelect = false;
                    grid.ColumnHeadersVisible = true;
                    grid.RowHeadersVisible = false;
                    grid.ScrollBars = ScrollBars.Vertical;
                    grid.Sort(grid.Columns[1], ListSortDirection.Ascending);
                    DataGridViewColumn column = grid.Columns[0];
                    column.Visible = false;
                    try
                    {
                        grid.CurrentCell = grid[1, 0];
                    }
                    catch { }
                    my = this.Parent.Parent as MyList;
                    my.label5.Text = grid.RowCount.ToString();
                    DataGridViewColumn column1 = grid.Columns[1];
                    column1.HeaderText = "WatchList";
                   // column1.DefaultCellStyle.Alignment = MiddleCenter;
                    column1.Width = 265;
                    sda.Update(datset);
                    grid.SelectionChanged += grid_SelectionChanged;
                    grid.CellMouseDown += grid_CellMouseDown;

                    if (grid.RowCount < 1)
                    {
                        my.OpenDetailsWL.Enabled = false;
                        //openToolStripMenuItem.Enabled = false;
                    }
                    else if (grid.RowCount > 0)
                    {
                        my.OpenDetailsWL.Enabled = true;
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("" + ex);
                }
            }
            conn.Close();
        }
    }



dgv 2

void loadDB()
    {
        SuspendLayout();
        string query = "SELECT ControlNum,TitleAnime FROM maindatabase.animelist";
        using (MySqlConnection conn = new MySqlConnection(myConnection))
        {
            conn.Open();
            using (MySqlCommand cmd = new MySqlCommand(query, conn))
            {
                try
                {

                    panel5.Controls.Add(grid);

                    sda = new MySqlDataAdapter();
                    sda.SelectCommand = cmd;
                    datset = new DataTable();
                    grid.DataSource = datset;
                    sda.Fill(datset);
                    bsource = new BindingSource();
                    grid.BackgroundImage = Properties.Resources.My;
                    grid.SetCellsTransparent();
                    grid.Width = 271;
                    grid.Height = 391;
                    grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    grid.AllowUserToDeleteRows = false;
                    grid.AllowUserToAddRows = false;
                    grid.AllowUserToResizeColumns = false;
                    grid.AllowUserToResizeRows = false;
                    grid.ReadOnly = true;
                    grid.AllowDrop = false;
                    grid.MultiSelect = false;
                    grid.ColumnHeadersVisible = false;
                    grid.RowHeadersVisible = false;
                    grid.ScrollBars = ScrollBars.Vertical;
                    grid.Sort(grid.Columns[1], ListSortDirection.Ascending);
                    //bsource.DataSource = datset;
                    //dataGridView1.DataSource = bsource;
                    DataGridViewColumn column = grid.Columns[0];
                    column.Visible = false;
                    try
                    {
                        grid.CurrentCell = grid[1, 0];
                    }
                    catch { }
                    label2.Text= grid.RowCount.ToString();
                    DataGridViewColumn column1 = grid.Columns[1];
                    column1.Width = 265;
                    column1.SortMode = DataGridViewColumnSortMode.Programmatic;
                    sda.Update(datset);
                    grid.SelectionChanged += grid_SelectionChanged;
                    grid.CellMouseDown += grid_CellMouseDown;
                    if (grid.RowCount < 1)
                    {
                        openToolStripMenuItem.Enabled = false;
                    }
                    else if (grid.RowCount > 0)
                    {
                        openToolStripMenuItem.Enabled = true;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("" + ex);
                }
            }
            conn.Close();
        }
        ResumeLayout();

1 个答案:

答案 0 :(得分:1)

您的列表1包含您的watchlist表格;您的列表2包含您的animelist表。

如果您从同一个数据库中检索这两个列表,并希望突出显示animelist列表中两个源表中的记录,则可以通过外部联接watchlist表来解决此问题:

dgv2查询:

SELECT
  animelist.ControlNum AS 'ControlNum',
  animelist.TitleAnime AS 'Title',
  IFNULL(watchlist.ControlNum, 0) AS 'WatchNum'
FROM maindatabase.animelist
LEFT JOIN maindatabase.watchlist ON watchlist.ControlNum = animelist.ControlNum

接下来,您必须在网格上注册一个事件处理程序来控制单元格格式化:

grid.CellFormatting += grid_CellFormatting;

事件处理程序将如下所示:

void grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (grid.Columns[e.ColumnIndex].Name == "ControlNum")
    {
        if (e.Value != null)
        {
            int controlnum;
            if (!int.TryParse((String)e.Value, out controlnum) || (controlnum < 1))
            {
                e.CellStyle.BackColor = Color.Red;
            }
        }
    }
}

A full example can be found on MSDN

我不是mysql pro,所以可以改进查询。