我有一个带有一些datagridviewimagecolumn的datagridview。 datagridview有cellcontentclick事件,因为它是一个图像列(这是正确的)。当我点击图像时,它会转到另一个对话框,以便我可以选择一个图像。到目前为止所有工作。
现在当它返回时,图像很好并显示出来。然后我做一个dataGridView2.ClearSelection()来取消选择所有单元格。接下来,如果我再次点击同一个单元格,则事件不会触发,但如果我单击另一个单元格,然后返回到此单元格,它将触发。我找了重置事件等但看不到有用的东西。我该如何解决这个问题?
以下是一些有用的代码:
点击单元格进入本节
else if (e.ColumnIndex.ToString() == "3")
{
try
{
display_label = new ChooseLabel(dataGridView2[3, e.RowIndex].Tag.ToString()); // open dalog to choose the format
display_label.ShowDialog(this); // this to get the dialog to open on the same screen as the app
//when the Display_Layouts dialog closes we retrieve the data and use it here
BitmapFilenameToDisplay = display_label.txtbx_layout_bitmap.Text;
Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
Label_Bitmap = new Bitmap(BitmapFilenameToDisplay);
dataGridView2[e.ColumnIndex, e.RowIndex].Value = Label_Bitmap.GetThumbnailImage(175, 100, myCallback, IntPtr.Zero); //create a bitmap of the image
// file_name = display_label.txtbx_layout_bitmap.Text; // bitmap to showtxtbx_layout_bitmap
dataGridView2[e.ColumnIndex, e.RowIndex].Tag = display_label.txtbx_choice.Text + ".bmp"; //BitmapFilenameToDisplay; // + ".bmp";
try
{
BitmapFilenameToDisplay = LayoutPath + dataGridView2[2, e.RowIndex].Tag.ToString();
}
catch
{
BitmapFilenameToDisplay = LayoutPath + "blank.bmp";
}
fill_in_graphics_data(e.RowIndex);
Layout_Bitmap = new Bitmap(BitmapFilenameToDisplay);
dataGridView2[1, e.RowIndex].Value = MergeTwoImages(Layout_Bitmap, Label_Bitmap).GetThumbnailImage(175, 100, myCallback, IntPtr.Zero);
dataGridView2.ClearSelection()
}
catch
{
showmessagebox(strings.label_preview_problem, "100");
//MessageBox.Show("Please try again to choose a Label Preview");
}
}
当我们输入此代码时,我们有
display_label = new ChooseLabel(dataGridView2[3, e.RowIndex].Tag.ToString()); // open dalog to choose the format
这会打开一个对话框,转到文件夹并显示文件夹中的所有位图,然后选择对话框并返回此处。接下来,您将看到打开的对话框的代码。
public ChooseLabel(string choosen_bitmap)
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
MainForm defaultpaths = new MainForm();
LayoutsPath = defaultpaths.txtbx_eggboxlabels_path.Text + "\\";
// string folder_path1 = Application.StartupPath; // get the current path //System.IO.Directory.GetCurrentDirectory();
string file_name = "";
// Format_bitmap_path = folder_path1 + "\\graphics\\" + nozzle + "\\" + format + "\\";
int grid_y = 0, grid_x = 0, index = 0, array_length = 0, num_rows = 1, num_bmp = 1;
DirectoryInfo d = new DirectoryInfo(LayoutsPath); //Path to the images
FileInfo[] Files = d.GetFiles("*.bmp"); //Getting image file names into an array
array_length = Files.Length;
int remainder = (array_length % num_col);
if (remainder != 0)
{
num_rows = (array_length / num_col) + 1; // add extra row as we have more bitmaps but not a full row.
}
else
{
num_rows = (array_length / num_col);
}
//loop round all the cells and make the NULL image blank.
foreach (var column in dataGridView1.Columns)
{
if (column is DataGridViewImageColumn)
(column as DataGridViewImageColumn).DefaultCellStyle.NullValue = null;
}
while (grid_x < num_rows)
{
grid_x = grid_x + 1;
dataGridView1.Rows.Add();
}
grid_x = 0;
while ((grid_y < num_col) & (num_bmp <= array_length))
{
file_name = Files[index].ToString();
Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
Bitmap myBitmap = new Bitmap(@LayoutsPath + file_name);
dataGridView1[grid_y, grid_x].Value = myBitmap.GetThumbnailImage(350, 200, myCallback, IntPtr.Zero); //create a bitmap of the image
dataGridView1[grid_y, grid_x].Tag = file_name.Substring(0, file_name.LastIndexOf('.')); //remove the extension and add to TAG
dataGridView1[grid_y+1, grid_x].Value = file_name.Substring(0, file_name.LastIndexOf('.')); //remove the extension and add to TAG
dataGridView1[grid_y + 1, grid_x].Tag = file_name.Substring(0, file_name.LastIndexOf('.'));
grid_y = grid_y + 1;
index = index + 1;
num_bmp = num_bmp + 1;
if ((num_bmp <= array_length) & (grid_y >= num_col))
{
grid_y = 0;
grid_x = grid_x + 1;
}
}
dataGridView1.AllowUserToAddRows = false; // add this so we do not get the extra row at the bottom
//fill in the data coming from the mainform.
string cell_to_highlight = choosen_bitmap.Substring(0, choosen_bitmap.LastIndexOf('.'));
txtbx_choice.Text = cell_to_highlight;
txtbx_layout_bitmap.Text = LayoutsPath + cell_to_highlight + ".bmp";
//work out which cell to highlight.
for (int i = 0; i < (dataGridView1.Rows.Count); i++)
{
if (dataGridView1.Rows[i].Cells[0].Tag.ToString() == cell_to_highlight)
{
dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
}
}
}
当我返回主网格单元格时,我专注于它,但点击相同的单元格似乎并没有触发事件。