Dear All,
Greetings of the day..
Please take a look at my problem. I want to get the CheckedListBox items Checked according to the database when I select an option from a DataGridView.
In a WinForm, I have a DataGridView contains User names (XYZ, ABC etc.) and a CheckedListBox contains different rights (Billing, Purchase, Stores, Edit, Delete etc.) to the user. a user (e.g. XYZ) rights saved (e.g. Purchase, Billing etc.) to MS Access database in a table...
UserName UserRights
XYZ Billing
XYZ Purchase
XYZ EditBills
Now, i want to see all the rights given to XYZ in the CheckedListBox when I select name XYZ from the DataGridView.
I got almost result but there is something missing that compares the db to CheckedListBox.
Please see the screen shots attached to get it more clearly. in this code (The Code.jpg) I only get first few items checked regardless, the actual rights.
Thank You.
Mayank Jani
Amit GuptaPosted Jan 19, 2018, 2:11 PM
Prashanth AkulaPosted May 25, 2018, 7:05 AM
Mayank JaniPosted Jan 21, 2018, 11:57 AM
{
var item = dsShowUserRights.Tables[0].Rows[x]["MenuName"].ToString();
for (int y = 0; y < chklbxUserRights.Items.Count; y++)
{
if (chklbxUserRights.Items[y].ToString() == item)
{
chklbxUserRights.SetItemChecked(y, true);
break;
}
}
Mayank JaniPosted Jan 19, 2018, 12:41 PM
Mayank JaniPosted Jan 12, 2018, 8:34 AM
Mayank JaniPosted Jan 12, 2018, 2:30 AM
{
//query for select user rights from the Access db. rights can be 5, 20, 25.
OleDbDataAdapter ShowUserRights = new OleDbDataAdapter("Select MenuName From UserRights Where UserName='" + dgvUsers.SelectedCells[0].Value + "'", MyConnDoc);
//suppose XYZ has 10 different menu rights, so this query will fetch 10 rows from the Access db.
DataSet dsShowUserRights = new DataSet();
ShowUserRights.Fill(dsShowUserRights);
if (dsShowUserRights.Tables[0].Rows.Count > 0)
{
//executing rows one by one.
for (int i = 0; i < dsShowUserRights.Tables[0].Rows.Count; i++)
{
//I get problem here, I want to compare the dataset rows with checkedlistitems as
//they are exactly same strings.
if(dsShowUserRights.Tables[0].Rows[0].ItemArray[0].Equals(chklbxUserRights.Items.ToString()))
{
//when match found, the checkedlistitem get checked
chklbxUserRights.SetItemChecked(i, true);
}
}
}
Suraj KumarPosted Jan 11, 2018, 3:02 PM