如何在执行DataGridView CellContentClick时为另一个控件触发事件?

时间:2011-12-23 09:24:31

标签: c# listview datagridview

实际上,我在DataGridView_CellContentClick上触发了一个事件,它执行与datagridview相关的操作,比如更改单元格的值但是在执行此操作之前,我想对另一个控件(即ListView)进行更改(或触发)另一个操作。但这不是虽然我在datagridview的操作之前放置了另一个操作,但仍然发生了。有人请帮帮我。

我的代码是这样的: -

    private void dGridDeviceList_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        //DataGridViewCell dcell = dGridDeviceList.Rows[e.RowIndex].Cells[e.ColumnIndex];
        if (e.RowIndex >= 0)
        {
            ListViewItem litem1 = lvInformation.Items.Add("101");

            litem1.SubItems.Add(string.Empty);
            litem1.SubItems[1].Text = "Connected.";

          ListViewItem litem5=  lvErrorList.Items.Add("check ");
            Cursor = Cursors.WaitCursor;
            List<cException> cxp = new List<cException>();
            cDeviceModel cdm = new cDeviceModel();
            ListViewItem litem = new ListViewItem();
            DataGridViewRow drow = new DataGridViewRow();
            cDeviceUtility cUtil = new cDeviceUtility();
            List<cAction> catn = new List<cAction>();
            drow = dGridDeviceList.Rows[e.RowIndex];
            cdm = (cDeviceModel)drow.Tag;
            if (e.ColumnIndex == 6)
            {

                if (dGridDeviceList.CurrentCell.Value.ToString() == "Connect")
                {
                    litem1= lvInformation.Items.Add("101");

                   litem1.SubItems.Add(string.Empty);
                   litem1.SubItems[1].Text = "Connected.";

                    //lvInformation.Items.Insert(0, "101");
                    //lvInformation.Items[0].SubItems.Add("Connected");
                }
                // connect disconnect button column
                if (cdm.IsConnected)
                {
                    ListViewItem litem2 = lvInformation.Items.Add("102");

                    litem2.SubItems.Add(string.Empty);
                    litem2.SubItems[1].Text =string.Format("Disconnecting from {0} device.",dGridDeviceList.CurrentRow.Cells["colDeviceName"].Value);

                    // then disconnect the device
                    cdm.IsConnected = false;
                    cdm.DeviceInterface.Disconnect();
                    dGridDeviceList.Rows[e.RowIndex].Tag = cdm;
                    dGridDeviceList.Rows[e.RowIndex].Cells[6].Value = "Connect";
                    dGridDeviceList.Rows[e.RowIndex].Cells[1].Value = img16x16.Images["notconnected"];
                    dGridDeviceList.Rows[e.RowIndex].Cells[8].Value= 0;
                    dGridDeviceList.Rows[e.RowIndex].Cells[8].Tag = "Not Connected";
                    dGridDeviceList.Refresh();
                    litem2 = lvInformation.Items.Add("103");
                    litem2.SubItems.Add(string.Empty);
                    litem2.SubItems[1].Text = string.Format("Disconnected from {0} device.", dGridDeviceList.CurrentRow.Cells["colDeviceName"].Value);
                }
                else
                {
                   // string test = lvInformation.Items[0].SubItems[1].ToString();
                   // catn.Add(new cAction { Number = lvInformation.Items.Count+1, Message = string.Format("Trying to connect with {0}", dGridDeviceList.Rows[e.RowIndex].Cells["colDeviceName"].Value) });
                   //// lvInformation.Items.Clear();
                   // foreach (cAction Actn in catn)
                   // {
                   //  litem=lvInformation.Items.Insert(0, (lvInformation.Items.Count + 1).ToString());
                   //    litem.SubItems.Add(catn[catn.Count -1].Message);
                   // }
                    // then connect the device
                    if (!ConnectToDevice(ref drow, out cxp) == true)
                    {
                        foreach (cException err in cxp)
                        {
                            litem = lvErrorList.Items.Insert(0, err.Number.ToString());
                            if (err.EType == ErrorType.Error)
                            {
                                litem.ImageKey = "error";
                            }
                            else if (err.EType == ErrorType.Warning)
                            {
                                litem.ImageKey = "warning";
                            }
                            else if (err.EType == ErrorType.Information)
                            {
                                litem.ImageKey = "information";
                            }
                            litem.SubItems.Add(err.Message);
                            litem.SubItems.Add(err.Source);
                        }
                    }
                    else
                    {
                        dGridDeviceList.Rows[e.RowIndex].Cells[0].Value = true;
                        dGridDeviceList.Rows[e.RowIndex].Tag = drow.Tag;
                        dGridDeviceList.Rows[e.RowIndex].Cells[6].Value = "Disconnect";
                        dGridDeviceList.Rows[e.RowIndex].Cells[1].Value = img16x16.Images["connected"];
                        dGridDeviceList.Rows[e.RowIndex].Cells[8].Value = 0;
                        dGridDeviceList.Rows[e.RowIndex].Cells[8].Tag = "Ready";
                        dGridDeviceList.Refresh();
                        RemoveSelectionRow();
                    }
                }
            }
            else if (e.ColumnIndex == 7)
            {
                // view logs button column
                pbarMain.Value = 100;
                ViewLogs(dGridDeviceList.Rows[e.RowIndex],ref lvAttnLog ,ref lvErrorList);
            }
            Cursor = Cursors.Arrow;
        }
    }

1 个答案:

答案 0 :(得分:1)

考虑到这是winform

 private void buttonCopyContent_Click(object sender, EventArgs e)
{
//do something
}

您可以通过以下行调用此活动  this.Invoke(new EventHandler(buttonCopyContent_Click));