question

BenTam avatar image
0 Votes"
BenTam asked BenTam commented

How to make a DataListView response to a mouse click

Dear All,

How to make a DataListView response to a mouse click? The following code doesn't work.

TIA

         private void Student_dataListView_CellClick(object sender, EventArgs e)
         {
             MessageBox.Show("a");
         }
dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Did you subscribe to the event?

0 Votes 0 ·

Hi @karenpayneoregon

I know how to subscribe to the event by reading JackJJun-MSFT's answer below. Thanks a lot!

0 Votes 0 ·

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@BenTam, If you want to get a response when you click anywhere from the datagirdview, you could subscribe the datagirdview_click event. Also If you want to get a response when you click the cell from the datagridview, you can try DataGridView1_CellClick event,

Here is a code example you could refer to.


 private void Form1_Load(object sender, EventArgs e)
         {
             DataTable table = new DataTable();
             table.Columns.Add("Name",typeof(string));
             table.Columns.Add("Age", typeof(int));
             table.Columns.Add("Id", typeof(int));
             table.Rows.Add("test1",23,1001);
             table.Rows.Add("test2", 25, 1002);
             table.Rows.Add("test3", 26, 1003);
             dataGridView1.DataSource = table;
             dataGridView1.Click += DataGridView1_Click;
             dataGridView1.CellClick += DataGridView1_CellClick;
                
    
         }
    
         private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
         {
             MessageBox.Show("datagirdview cell is clicked");
         }
    
         private void DataGridView1_Click(object sender, EventArgs e)
         {
             MessageBox.Show("datagirdview is clicked");
         }

Please note that we need to unsubscribe the DataGridView1_Click If we want to use Cell_Click because it will cover the Cell_Click event.

Hope this could help you.




If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.