如何避免重复代码以提高效率
我有一个 DataGrid
view1 和一个 ListView
,每当我选择列表视图项时(我将 ListView
项传递到查询中并填充根据该项目的 DataGrid
视图)
我已经编写了一些这样的代码......
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
}
我已经像上面那样完成了......我认为这不是一种有效的编码方式。而且这段代码由很多重复的行组成,有什么办法可以将这段代码折射成一小堆代码...... 为了提高效率?
任何提高代码效率的想法和示例片段都会对我有所帮助...
提前非常感谢...
我正在使用 c# 并编写 WinForms 应用程序.....
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.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将委托保存到列表视图项中。并在选择封装项时调用它。例如,您将像这样填充列表框:
现在,当选择此项时,您将执行如下方法:
注意:!尚未测试此代码,但类似的内容应该可以工作,并且您不必编写 If 语句,您只需正确构造项目即可。
另请注意,对于刚接触此代码的人来说,这可能有点难以阅读。
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:
Now, when this item gets selected, you execute the method like so:
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.
您可以轻松地提取一个新方法来执行“datagrid view1 绑定”。然后从所有需要进行绑定的方法中调用此方法。
You could easily extract a new method to do the "datagrid view1 binding". This method is then called from all the methods that need to do the binding.