简体   繁体   中英

Getting Selected Index With DataGridView Using MVP and Winforms

I am creating a database-driven application using winforms and the MVP(Model View Presenter) pattern and I'm running into an issue that I cannot seem to solve. I have wriiten a data access layer which pulls all my customer information from a table and stores it into a List. Now since I'm using the MVP pattern my datagrid view is an object, I have bound my List in my data access layer so it displays in the datagris view when I press the "View" button but what I'm having difficulty with is getting the selected index and displaying that row of information inside textboxes on another form. If I wasn't using the MVP pattern I could use the selectedIndex property but since my view properities are using interfaces I am not sure how I would go about getting the slected index. Here is my MVP layout:

View:

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
       }

Presenter: 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
       }

Model: 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
        }

Data Access Layer Method:

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
           }

I realise that some part of the code could be cleaned up but I wanted to give you a general idea of how I'm laying out my architecture. As for solutions I have tried to loop through the datagrid and get the index basied on that and creating a get method to return the current row (which works but I can't set values to it with it being read only) but I can't see a clean way which doesn't break the MVP pattern.

I should clarify that I'm not looking for complete code solutions but rather to be pointed in the right direction.

Thank you

SelectedIndex returns an int .

Let's call this int currentSelectionIndex .

When the View button is clicked, the event fires a method that gets currentSelectionIndex and sends it to your Data Access Layer that contains the customerList .

Returning customerList[currentSelectionIndex] will return the currently selected CustomerModel, provided that customerList is in the same sorting order as the datagrid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM