C# 使用MVP和Winforms使用DataGridView获取选定索引
我正在使用winforms和MVP(Model View Presenter)模式创建一个数据库驱动的应用程序,我遇到了一个似乎无法解决的问题。我编写了一个数据访问层,它从一个表中提取所有客户信息,并将其存储到一个列表中。现在,由于我使用MVP模式,我的datagrid视图是一个对象,我在数据访问层中绑定了我的列表,因此当我按下“view”按钮时,它会显示在datagris视图中,但我遇到的困难是获取所选索引并在另一个表单的文本框中显示该行信息。如果我没有使用MVP模式,我可以使用selectedIndex属性,但由于我的视图属性使用的是接口,我不确定如何获取选定的索引。以下是我的MVP布局: 视图: 01 数据访问层方法: 01C# 使用MVP和Winforms使用DataGridView获取选定索引,c#,winforms,mvp,C#,Winforms,Mvp,我正在使用winforms和MVP(Model View Presenter)模式创建一个数据库驱动的应用程序,我遇到了一个似乎无法解决的问题。我编写了一个数据访问层,它从一个表中提取所有客户信息,并将其存储到一个列表中。现在,由于我使用MVP模式,我的datagrid视图是一个对象,我在数据访问层中绑定了我的列表,因此当我按下“view”按钮时,它会显示在datagris视图中,但我遇到的困难是获取所选索引并在另一个表单的文本框中显示该行信息。如果我没有使用MVP模式,我可以使用selecte
公共列表GetAllCustomers()
02
{
03
List customerList=新列表();
04
05
尝试
06
{
07
_command.CommandText=“选择*来自客户”;
08
_command.CommandType=CommandType.Text;
09
_connection.Open();
10
11
OleDbDataReader=_命令.ExecuteReader();
12
13
while(reader.Read())
14
{
15
CustomerModel CustomerModel=新CustomerModel();
16
17
customerModel.ID=Convert.ToInt32(读卡器[“custid”]).ToString();
18
customerModel.Title=reader[“Title”].ToString();
19
customerModel.FirstName=读取器[“FirstName”].ToString();
20
customerModel.LastName=读取器[“LastName”].ToString();
21
customerModel.DateOfBith=reader[“dob”].ToString();
22
customerModel.Natins=reader[“Natins”].ToString();
23
customerModel.Email=reader[“Email”].ToString();
24
customerModel.allowment=Convert.ToDouble(读卡器[“allowment”]).ToString();
25
26
customerList.Add(customerModel);
27
}
28
}
29
捕获(例外情况除外)
30
{
31
消息=例如消息;
32
}
33
最后
34
{
35
CloseDatabase();
36
37
}
38
39
返回客户列表;
40
}
我意识到代码的某些部分是可以清理的,但我想让您大致了解我是如何布置我的体系结构的。至于解决方案,我尝试在datagrid中循环,并在此基础上获取索引,并创建一个get方法来返回当前行(该方法可以工作,但我不能将其设置为只读),但我看不到一种干净的方法不打破MVP模式
我应该澄清的是,我不是在寻找完整的代码解决方案,而是要指出正确的方向
谢谢
SelectedIndex
返回一个int
让我们将其称为int-currentSelectionIndex
单击View按钮时,事件触发一个获取currentSelectionIndex
的方法,并将其发送到包含customerList
的数据访问层
返回customerList[currentSelectionIndex]
将返回当前选择的CustomerModel,前提是customerList
与数据网格的排序顺序相同
public interface IHomeView
02
{
03
string SearchText { get; set; }
04
string ConnectionStatus { get; set; }
05
object ViewTables { get; set; }
06
object DataGridCustomers { get; set; }
07
object DataGridCustomersSelectedRow { get; set; }
08
event EventHandler<EventArgs> PopulateData;
09
event EventHandler<EventArgs> SearchData;
10
event EventHandler<EventArgs> PopulateSearchData;
11
event EventHandler<EventArgs> Connectionstatus;
12
event EventHandler<EventArgs> DisplayForm;
13
}
public class HomePresenter
02
{
03
private readonly IHomeView view;
04
05
private SearchRepository repository;
06
private CustomerRepository customerRepository;
07
private List<CustomerModel> customerModel;
08
09
public HomePresenter(IHomeView view)
10
{
11
this.view = view;
12
InitialiseEvents();
13
repository = new SearchRepository();
14
customerRepository = new CustomerRepository();
15
customerModel = customerRepository.GetAllCustomers();
16
}
17
18
private void InitialiseEvents()
19
{
20
view.PopulateData += PopulateData;
21
view.SearchData += SearchData;
22
view.PopulateSearchData += PopulateSearchCriteria;
23
view.Connectionstatus += ConnectionStatus;
24
view.DisplayForm += DisplayForm;
25
}
26
27
private void PopulateData(object sender, EventArgs e)
28
{
29
try
30
{
31
try
32
{
33
//view.DataGridCustomers = customerRepository.GetAllCustomers().Where(value => cm.LastName.Equals(view.SearchText)).ToList();
34
view.DataGridCustomers = customerRepository.GetAllCustomers();
35
36
}
37
catch (Exception ex)
38
{
39
40
}
41
}
42
catch (Exception ex)
43
{
44
45
}
46
finally
47
{
48
//MessageBox.Show(customerRepository.Message);
49
}
50
51
}
52
53
private void SearchData(object sender, EventArgs e)
54
{
55
if (view.ViewTables != null)
56
{
57
view.DataGridCustomers = customerRepository.GetAllCustomers().Where(cm => cm.LastName.Equals(view.SearchText)).ToList();
58
59
60
}
61
else
62
{
63
MessageBox.Show("Please select a table");
64
}
65
}
66
67
private void PopulateSearchCriteria(object sender, EventArgs e)
68
{
69
if (view.ViewTables != null)
70
{
71
}
72
}
73
74
private void ConnectionStatus(object sender, EventArgs e)
75
{
76
if (repository != null)
77
{
78
view.ConnectionStatus = "Online";
79
}
80
else
81
{
82
view.ConnectionStatus = "Offline";
83
}
84
}
85
86
private void DisplayForm(object sender, EventArgs e)
87
{
88
frmCustomer customerView = new frmCustomer();
89
customerView.ShowDialog();
90
}
91
}
public class CustomerModel
02
{
03
public string ID { get; set; }
04
public string Title { get; set; }
05
public string FirstName { get; set; }
06
public string LastName { get; set; }
07
public string DateOfBith { get; set; }
08
public string Natins { get; set; }
09
public string Email { get; set; }
10
public string Allowance { get; set; }
11
}
public List<CustomerModel> GetAllCustomers()
02
{
03
List<CustomerModel> customerList = new List<CustomerModel>();
04
05
try
06
{
07
_command.CommandText = "SELECT * FROM customer";
08
_command.CommandType = CommandType.Text;
09
_connection.Open();
10
11
OleDbDataReader reader = _command.ExecuteReader();
12
13
while (reader.Read())
14
{
15
CustomerModel customerModel = new CustomerModel();
16
17
customerModel.ID = Convert.ToInt32(reader["custid"]).ToString();
18
customerModel.Title = reader["title"].ToString();
19
customerModel.FirstName = reader["firstname"].ToString();
20
customerModel.LastName = reader["lastname"].ToString();
21
customerModel.DateOfBith = reader["dob"].ToString();
22
customerModel.Natins = reader["natins"].ToString();
23
customerModel.Email = reader["email"].ToString();
24
customerModel.Allowance = Convert.ToDouble(reader["allowance"]).ToString();
25
26
customerList.Add(customerModel);
27
}
28
}
29
catch (Exception ex)
30
{
31
Message = ex.Message;
32
}
33
finally
34
{
35
CloseDatabase();
36
37
}
38
39
return customerList;
40
}