I have a DataGrid view1 and ListView , and when I select a list item (I pass the ListView element to the request and populate the DataGrid view with that element)
I wrote 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 did this as described above ... I think this is not an efficient way of coding. and this code contains many repeating lines, is there any way to refract this code to a small group of code ...... in order to increase efficiency?
Any ideas and examples of snippets to improve code efficiency would be helpful to me ...
Thank you very much in advance....
I am using C # and calling WinForms applications .....
source share