【问题标题】:Datagridview c# with SQL for Professional DeveloperDatagridview c# with SQL for Professional Developer
【发布时间】:2015-04-14 01:34:24
【问题描述】:

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

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

定时器代码:

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

【问题讨论】:

  • 我了解该应用程序,但不完全了解您面临的问题。 “听者叫”是什么意思?你能帮我理解一下吗?
  • 除了主要问题之外,您的计时器滴答事件与_RefreshGrid 完全相同,但您也可以调用_RefreshGrid。代码可以完全替换为_RefreshGrid。在回答完这个问题后,我建议您前往 codereview.stackexchange.com 并在那里发布代码以获取有关优化它的更多信息
  • 当这个函数调用 private void dataGridView1_CellValueChanged_1(object sender, DataGridViewCellEventArgs e) 时发生问题,每次我填充网格视图时都会调用这个函数我需要它只在鼠标单击复选框时运行 Thnks

标签: c# sql datagridview


【解决方案1】:

我用下面的代码解决了这个问题:

  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 更改监听器的类型 至 私有 void dataGridView1_CellContentClick

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 2011-03-02
    相关资源
    最近更新 更多