如何在执行DataGridView_CellContentClick时为另一个控件触发事件
本文关键字:另一个 控件 事件 CellContentClick 执行 DataGridView | 更新日期: 2023-09-27 18:18:44
实际上,我已经在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;
}
}
考虑到这是winform
private void buttonCopyContent_Click(object sender, EventArgs e)
{
//do something
}
您可以通过以下行调用此事件this.Invoke(new EventHandler(buttonCopyContent_Click));