DataListView: How to use ENTER for mouse double-clicking

BenTam 1,501 Reputation points
2021-11-04T04:26:36.25+00:00

Dear All,

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

TIA

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,099 questions
0 comments No comments
{count} votes

Accepted answer
  1. ShuaiHua Du 636 Reputation points
    2021-11-05T04:05:50.627+00:00

    @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!!!

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Jack J Jun 24,276 Reputation points Microsoft Vendor
    2021-11-04T06:15:38.083+00:00

    @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. ShuaiHua Du 636 Reputation points
    2021-11-04T14:31:02.177+00:00

    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!!!


  3. BenTam 1,501 Reputation points
    2021-11-05T14:20:56.583+00:00

    Hi @ShuaiHua Du

    Thanks for your code. It works.

    0 comments No comments