question

BenTam avatar image
0 Votes"
BenTam asked BenTam answered

DataListView: How to use ENTER for mouse double-clicking

Dear All,

When I click at a row of a DataListView, how to get the row number?

TIA

dotnet-csharp
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.

ShuaiHuaDu avatar image
0 Votes"
ShuaiHuaDu answered

@BenTam Thanks for your information.

I tried the DataListView, the code as below:

 private void dataListView1_MouseDoubleClick(object sender, MouseEventArgs e)
 {
     var dlv = sender as DataListView;
     MessageBox.Show(dlv.SelectedIndex.ToString());
     MessageBox.Show(string.Join(",", GetListViewSelectedIndexes(dlv.SelectedIndices)));
 }
    
 private void dataListView1_DoubleClick(object sender, EventArgs e)
 {
     var dlv = sender as DataListView;
     MessageBox.Show(dlv.SelectedIndex.ToString());
     MessageBox.Show(string.Join(",", GetListViewSelectedIndexes(dlv.SelectedIndices)));
 }
    
 private List<int> GetListViewSelectedIndexes(SelectedIndexCollection collection)
 {
     var indexes = Enumerable.Empty<int>().ToList();
     for (int i = 0; i < collection.Count; i++)
     {
         indexes.Add(collection[i]);
     }
     return indexes;
 }

Beacause the DataListView can be multiple selected, that suggest you use SelectedIndices.

If right, please accept.
Enjoy Programming!!!

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.

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

@BenTam, you could try the following code to get the row number when you use the Mouse-Double click event.


 public Form1()
         {
             InitializeComponent();
               
             dataGridView1.MouseDoubleClick += DataGridView1_MouseDoubleClick;
         }
    
         private void DataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
         {
             MessageBox.Show(dataGridView1.CurrentCell.RowIndex.ToString());
         }

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.


· 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.

@JackJJun-MSFT

I follow your suggestion and got an error (see the following screen capture). I note that you changed my DataListView to dataGridView (including the prior answers). I wonder DataListView does not have the CurrentCell property. Please help!



146480-error.gif


0 Votes 0 ·
error.gif (4.9 KiB)

@BenTam,as others said, there is no DataListView in winform, I thought the DataListView you mentioned is DataGirdview. Then. how do you create the DataListView in winform? Is it a Listview? Could you provide a picture about the control?

0 Votes 0 ·
ShuaiHuaDu avatar image
0 Votes"
ShuaiHuaDu answered ShuaiHuaDu commented

There is no DataListView in WinForm App.

I think you are using ListView, there are two double click events of ListView.

Get the index when double click, code as below:

 private void lvTest_DoubleClick(object sender, EventArgs e)
 {
     var listView = sender as ListView;
     MessageBox.Show(string.Join(",", GetListViewSelectedIndexes(listView.SelectedIndices)));
 }
    
 private void lvTest_MouseDoubleClick(object sender, MouseEventArgs e)
 {
     var listView = sender as ListView;
     MessageBox.Show(string.Join(",", GetListViewSelectedIndexes(listView.SelectedIndices)));
 }
    
 private List<int> GetListViewSelectedIndexes(SelectedIndexCollection collection)
 {
     var indexes = Enumerable.Empty<int>().ToList();
     for (int i = 0; i < collection.Count; i++)
     {
         indexes.Add(collection[i]);
     }
     return indexes;
 }

If right, please accept.

Enjoy Programming!!!

· 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.

Hi @ShuaiHuaDu and @JackJJun-MSFT

Thanks for your replies. DataListView is a subclass of ObjectListView, which was created by Phillip Piper. See http://objectlistview.sourceforge.net/cs/whatsnew.html and press CTRL+F to search DataListView. Anyways, thanks for your support.


0 Votes 0 ·

Please view my new answer, if any question feel free to comment.

0 Votes 0 ·
BenTam avatar image
0 Votes"
BenTam answered

Hi @ShuaiHuaDu

Thanks for your code. It works.

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.