我正在使用winforms和MVP(模型视图展示器)模式创建一个数据库驱动的应用程序,我遇到了一个我似乎无法解决的问题。我已经获得了一个数据访问层,它从表中提取所有客户信息并将其存储到List中。现在因为我正在使用MVP模式我的数据网格视图是一个对象,我已经将我的List绑定在我的数据访问层中,所以当我按下“查看”按钮时它会显示在datagris视图中,但我遇到的困难是获取所选索引并在另一个表单上的文本框中显示该行信息。如果我没有使用MVP模式,我可以使用selectedIndex属性,但由于我的视图特性是使用接口,我不知道如何获取被选中的索引。这是我的MVP布局:
查看:
01
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
}
主讲人: 01
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
}
型号: 01
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
}
数据访问层方法:
01
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
}
我意识到代码的某些部分可以清理,但我想让你大致了解我如何布置我的架构。至于解决方案,我试图循环遍历数据网格并获取基于此的索引,并创建一个get方法来返回当前行(它可以工作,但我无法设置值,只读它)但我可以'看到一个不破坏MVP模式的干净方式。
我应该澄清一点,我不是在寻找完整的代码解决方案,而是指向正确的方向。
谢谢
答案 0 :(得分:0)
[cos(x), cos(x)*sin(y)]
会返回string xml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<folder name=\"c\">" +
"<folder name=\"program files\">" +
"<folder name=\"uninstall information\" />" +
"</folder>" +
"<folder name=\"users\" />" +
"</folder>";
XElement xElement = XElement.Parse(xml);
IEnumerable<string> values = xElement.
Descendants("folder").
Where(element => element.Attribute("name")?.Value?.StartsWith("u") == true).
Select(element => element.Attribute("name").Value);
。
我们称之为SelectedIndex
。
单击“查看”按钮时,该事件会触发获取int
的方法并将其发送到包含int currentSelectionIndex
的数据访问层。
如果currentSelectionIndex
与数据网格的排序顺序相同,则返回customerList
将返回当前选定的CustomerModel。