Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6377045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:49:16+00:00 2026-05-25T01:49:16+00:00

I have a DataGrid view1 and a ListView and when ever I select the

  • 0

I have a DataGrid view1 and a ListView and when ever I select the list view item(I am passing the ListView item into the query and populating the DataGrid view according that item)

I have wrote some code like this….

 private void listview_selectedindexchanged(object sender event args)
 {
     if (listview.SelectedItems.Count > 0 && listview.SelectedItems[0].Group.Name == "abc")
     {
            if(lstview.SelectedItems[0].Text.ToString() == "sfs")
            {
              method1();
            }
            else
            {
                // datagrid view1 binding
               blah.....
             }
     }
     if (lstview.SelectedItems.Count > 0 && lstview.SelectedItems[0].Group.Name == "def")
     {

           if(lstview.SelectedItems[0].Text.ToString() == "xyz")
           {
               method 1();
           }
           if(lstview.SelectedItems[0].Text.ToString() == "ghi")
           {
               method 2(a,b);
           }
           if(lstview.SelectedItems[0].Text.ToString() == "jkl")
           {
               method 2(c,d);
           }
           if(lstview.SelectedItems[0].Text.ToString() == "mno")
           {
               method 3();
           }

       }
   }  
private void method 1()
{ 
  // datagrid view1 binding
    blahh     
}
private void method 2(e,g)
{
  // datagrid view1 binding
  blah....blah..
}
private void method 3()
{

    // datagrid view1 binding
}

I have done it like above … I think this is not an efficient way to do the coding. and this code consisits of a lot of repeated lines, is there any way to refractor this code to a small bunch of code ……
in order improve the efficiency?

Any ideas and sample snippets for increasing code efficiency would be helpful to me …

Many thanks in advance….

I am using c# and writting WinForms applications…..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:49:17+00:00Added an answer on May 25, 2026 at 1:49 am

    You could save a delegate into the listview item. And call it when the encapsulating item gets selected. For example you would fill your listbox like this:

    ListViewItem item = new ListViewItem("abc");
    item.Tag = new Delegate(method1);
    lstview.Items.Add(item);
    

    Now, when this item gets selected, you execute the method like so:

    private void listview_selectedindexchanged(object sender event args)
    {
        ((Delegate)lstview.SelectedItems[0].Tag)(); // this will execute method1 if the item with text "abc" gets selected
    }
    

    NOTE: ! have not tested this code, but something along those lines should work and you don’t have to write the If-statement, you only have to construct the items correctly.

    Also note that this may be a bit hard to read for someone new to this code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom control that is derived from ListView . This list view
My main form in my application has a datagrid view that can have 1
I have a datagrid view that is bind to a custom collection. I have
Currently i have a datagrid view that displays names in the 1st columnnd in
i have datagrid view with products the datagrid view will be loaded according to
I have added button to datagrid view but when ever the function is called
I have this requierements that if a click a particular cell on datagrid view..it
I have a Data Grid View inside a control that is displayed in a
I have a DataGridView that I want to query using Linq (C# WinForm). I
Im using c# .net windows form application. i have a datagrid view. It has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.