Datagridview c#with SQL for Professional Developer

时间:2015-04-13 07:53:40

标签: c# sql datagridview

首先,我将简要介绍一下我的应用程序:

  1. C#应用程序使用sql server来共享"任务"团队之间
  2. 所有用户都可以看到任务
  3. 当用户选中复选框
  4. 时会发生事件
  5. 我使用计时器每4秒刷新一次网格
  6. 计时器代码:

    private static int second = 0;
    
    private void timer1_Tick(object sender, EventArgs e)
    {
            try
            {
                second++;
                List<Tasks> list=taskController.taskController.GetList();
                _FillGridFromList(list);
                _RefreshGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    }
    
    /*reFill the grid*/
    private void _RefreshGrid()
    {
            try
            {
                List<Tasks> list = taskController.taskController.GetList();
                _FillGridFromList(list);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "Unable to Retrive Data from Server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    }
    
    /*This function will fill userGridView from Arraylist that get from SQL */
    private void _FillGridFromList(List<Tasks> list)
    {
            dataGridView1.ClearSelection();
    
            try
            {
                DataTable table1 = new DataTable("sheet");
                System.Data.DataSet tmpSet = new System.Data.DataSet();
                DataRow row = table1.NewRow();
    
                table1.Columns.Add("ID");
                table1.Columns.Add("Title");
                table1.Columns.Add("Description");
                table1.Columns.Add("Preiority");
                table1.Columns.Add("Status");
                table1.Columns.Add("Done By");
                table1.Columns.Add("Start Date");
    
    
                int j = 0;
                for (int i = 0; i < list.Count; i++)
                {
                    Tasks tmp = (Tasks)list[i];
                    row[j++] = tmp.ID;
                    row[j++] = tmp.Name;
                    row[j++] = tmp.Description;
                    row[j++] = tmp.Preiority;
                    row[j++] = tmp.Status;
                    row[j++] = tmp.Done_By;
                    row[j++] = tmp.Start_Date;
    
    
                    j = 0;
                    table1.Rows.Add(row);
                    row = table1.NewRow();
                }
    
                tmpSet.Tables.Add(table1);
                dataGridView1.DataSource = tmpSet.Tables[0];
               // dataGridView1.ReadOnly = true;
                dataGridView1.Columns[1].ReadOnly = true;
                dataGridView1.Columns[2].ReadOnly = true;
                dataGridView1.Columns[3].ReadOnly = true;
                dataGridView1.Columns[4].ReadOnly = true;
                dataGridView1.Columns[5].ReadOnly = true;
                dataGridView1.Columns[6].ReadOnly = true;
                dataGridView1.Columns[7].ReadOnly = true;
    
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].Done_By.Trim().Length > 0)
                    {
                        dataGridView1.Rows[i].Cells[0].Value = true;
    
                        if (list[i].Done_By != utility.Util.User.Display_Name)
                        {
                            dataGridView1.Rows[i].Cells[0].ReadOnly = true;
                        }
                        else
                        {
                            dataGridView1.Rows[i].Cells[0].ReadOnly = false;
                        }
                    }
                    else
                    {
                        dataGridView1.Rows[i].Cells[0].Value = false;
                    }
    
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(this, e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    }
    
    private void dataGridView1_CellValueChanged_1(object sender, DataGridViewCellEventArgs e)
    {
            try
            {
                if(second > 5)
                {
                    foreach (DataGridViewRow dr in dataGridView1.Rows)
                    {
                        DataGridViewCheckBoxCell drx = (DataGridViewCheckBoxCell)dr.Cells[0];
                        if(sender == drx)
    
                        if (drx.Selected != null) //Cells[0] Because in cell 0th cell we have added checkbox
                        {
                            if (drx.Selected==true && dr.Cells[6].Value.ToString() == utility.Util.User.Display_Name)
                            {
                                Tasks tsk = taskController.taskController.Read(dr.Cells[1].Value + "");
                                {
                                    tsk.Done_By = "";
                                    tsk.Status = "";
                                    taskController.taskController.Update(tsk);
                                }
                            }
                            else
                            {
                                if (drx.Selected !=true)
                                {
                                    Tasks tsk = taskController.taskController.Read(dr.Cells[1].Value + "");
    
                                        tsk.Done_By = utility.Util.User.Display_Name;
                                        tsk.Status = "Closed";
                                        taskController.taskController.Update(tsk);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    }
    

    主要问题是,当我再次填充表单时,会调用侦听器,因为我重建了gridview。

    http://i.stack.imgur.com/l0Rdv.png

1 个答案:

答案 0 :(得分:0)

我使用以下代码解决了这个问题:

  private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        //MessageBox.Show(e.RowIndex+"   "+e.ColumnIndex);

     //   Int32 selectedRowCount = dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected);
       // Int32 selectedColumnCount = dataGridView1.Columns.GetColumnCount(DataGridViewElementStates.Selected);

        if ( e.ColumnIndex == 0  && e.RowIndex >= 0  )
        {
            string x=dataGridView1.Rows[e.RowIndex-1].Cells[6].Value.ToString();
            if ((bool)dataGridView1.Rows[e.RowIndex-1].Cells[0].Value == false && x!=null )
            {
                if(x==Util.User.Display_Name)
                {
                    Tasks tsk = taskController.taskController.Read(dataGridView1.Rows[e.RowIndex-1].Cells[1].Value + "");
                    tsk.Done_By = "";
                    tsk.Status = "";
                    taskController.taskController.Update(tsk);
                    dataGridView1.Rows[0].Cells[1].Value = true;
                }
            }
            else
            {
                if ((bool)dataGridView1.Rows[e.RowIndex-1].Cells[0].Value == true)
                {
                    Tasks tsk = taskController.taskController.Read(dataGridView1.Rows[e.RowIndex-1].Cells[1].Value + "");

                    tsk.Done_By = utility.Util.User.Display_Name;
                    tsk.Status = "Closed";
                    taskController.taskController.Update(tsk);
                }
            }


        }
    }

我只更改私有void dataGridView1_CellValueChanged_1的监听器类型 至   private void dataGridView1_CellContentClick

相关问题