Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

How to sort data inside datagridview in virtual Mode?

$
0
0

I am a student. working with datagridview virtual mode. having 2 combobox 

combobox1- have 2 options Show all data and select role;

show all data load the data from database inside dictionary through paggination and then cell value needed implemented

when option select role is selected combobox 2 is displayed and it is bind to role dictionary. 

i have a column inside datagridview role and wanted to sort the data on the basis of role. the basic problem is in when datagridview is going to cell value needed it is showing dictionary  key not found which is basically row index in   void CboBoxSortingDatagridview(ComboBox sender) function;

the code which i do is

private void cbochoice_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            if (cboChoice.SelectedIndex == 0)
            {
                lblSelectRoles.Visible = false;
                cboRolesList.Visible = false;

                DictionaryGeneralUser = null;
            }
            else
            {

                cboRolesList.DataSource = new BindingSource(dictionaryRole,null);
                cboRolesList.DisplayMember = "Value";
                cboRolesList.ValueMember = "Key";
                

                lblSelectRoles.Visible = true;
                cboRolesList.Visible = true;

                if (cboRolesList.DataSource != null)
                {
                    cboRolesList.SelectedIndex = 0;
                }

            }
        }

    private void dataGridViewMain_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            GeneralUser generalUsersTemp = null;
            GeneralUserDetails generalUserDetailsTemp = null;

            if (!DictionaryCellValueNeeded.ContainsKey(e.RowIndex))
            {
                int rowIndex = e.RowIndex;
                PopulateItemsUsingPaggingInDataGridView_Main(rowIndex);
            }

            if (DictionaryGeneralUser != null)
            {
                dataGridViewMain.Refresh();
                generalUsersTemp = (GeneralUser)this.DictionaryGeneralUser[e.RowIndex];
            }
            else
                generalUsersTemp = (GeneralUser)this.DictionaryCellValueNeeded[e.RowIndex];


            if (generalUsersTemp.dictionaryUserDetails.ContainsKey(e.RowIndex))
            {
                generalUserDetailsTemp = (GeneralUserDetails)generalUsersTemp.dictionaryUserDetails[e.RowIndex];

            }

            switch (this.dataGridViewMain.Columns[e.ColumnIndex].Name)
            {
                case "Username":
                    e.Value = generalUsersTemp.Username;
                    break;
                case "Role":
                    foreach (var v in dictionaryRole)
                    {
                        if (v.Key == generalUsersTemp.RoleId)
                            e.Value = v.Value.ToString();
                    }
                    break;
                case "Password":
                    e.Value = generalUsersTemp.Password;
                    break;
                case "Permission":
                    e.Value = generalUsersTemp.Permission;
                    break;
                case "Name":
                    e.Value = generalUserDetailsTemp.Name;
                    break;
                case "Email":
                    e.Value = generalUserDetailsTemp.Email;
                    break;
                case "Phone":
                    e.Value = generalUserDetailsTemp.ContactNo;
                    break;
                case "Address":
                    e.Value = generalUserDetailsTemp.Address;
                    break;
                case "NIC":
                    e.Value = generalUserDetailsTemp.Nic;
                    break;
                case "Gender":
                    e.Value = generalUserDetailsTemp.Gender;
                    break;
            }
        }

private void cboRolesList_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboRolesList.SelectedIndex)
            {
                case 0:
            case 1:
                case 2:
                case 3: CboBoxSortingDatagridview(cboRolesList); break;
            }
        }

        

        void CboBoxSortingDatagridview(ComboBox sender)
        {
            
            
            
           Dictionary<int,GeneralUser> filterUserDictionary = new Dictionary<int, GeneralUser>();
            GeneralUser gu = new GeneralUser();
            GeneralUser gu1 = new GeneralUser();
            GeneralUserDetails gud = new GeneralUserDetails();
            
            
            int count = 0;

            KeyValuePair<int, string> selectedEntry = (KeyValuePair<int, string>)((ComboBox)sender).SelectedItem;
            foreach (var v in DictionaryCellValueNeeded)
            {
                if (!filterUserDictionary.ContainsKey(v.Key) && v.Value.RoleId == selectedEntry.Key)
                {
                    gu = (GeneralUser)this.DictionaryCellValueNeeded[v.Key];

                    if (gu.dictionaryUserDetails.ContainsKey(v.Key))
                    {
                        gud = (GeneralUserDetails)gu.dictionaryUserDetails[v.Key];
                    }
                    gu1.Password = gu.Password;
                    gu1.Permission = gu.Permission;
                    gu1.RoleId = gu.RoleId;
                    gu1.UserId = gu.UserId;
                    gu1.Username = gu.Username;
                    gu1.dictionaryUserDetails.Add(count, (GeneralUserDetails)gud);
                    filterUserDictionary.Add(count++, (GeneralUser)gu1);
                }
            }
            DictionaryGeneralUser = filterUserDictionary;
           
            dataGridViewMain.DataSource = DictionaryGeneralUser.Values.ToList();
            
        }


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>