Thursday, March 8, 2012

Data binding problem

Hi Guys,

I am having a little problem with databinding with multiple tables. I have two tables in my db -

Categories

-

CategoryID CategoryName

SubCategories

SCategoryID SCategoryName CategoryID

I have a stored procedure to select SCategoryID, SCategoryName, CategoryName

And I have a form with 2 textboxes, a combobox, and a grid control. I load the resultset of the stored procedure in a dataset, and bind the grid and textboxes (SCategoryID, SCAtegoryName) with the dataset. I fill the combobox with the categories table and set the DisplayMember = CategoryName, ValueMember = CategoryID.

Now the problem is, I can't bind the combobox to the subcategories so that when I select a record in the grid, the combobox changes accordingly.

How can I achieve this?

Regards

Kapalic

I'm assuming you're developing a Windows application and that you are handling the SelectionChanged event of the data grid to load the form with the data of the selected item. You should have already loaded the data grid with the sub categories and loaded the combo box with the categories in the form load method. To display the category of the selected sub category, you should set the "SelectedValue" of the combo box to the category ID of the selected item.

If this doesn't answer your question, please post your code so that I can check it for you.

Best regards,

Sami Samir

|||

Dear Sami

Yet again, that solves my problem! I really don't know how to thank you!!

Yes, the scenario you tell is all right. I loaded a combobox with categories, and the grid with Sub Category ID, Name, and Category Name. Problem I binding the combo with the grid to reflect the change when selection changes.

I was handling it in the CellClick event and KeyDown event of the property to reflect the change in the combo manually, not being succed to bind it properly. And problem hapening. When I click the cell, combo changes successfully. But when I use the arrows, it was not showing proper value. Sometime showing the current cell value, sometime the previous cell value.

Here is the code I use in two events -

Code Snippet

With dgvResults
cboCategory.SelectedIndex = cboCategory.FindString(.CurrentRow.Cells(2).Value.ToString)
End With

But now I use it only in the SelectionChanged event, and it works nicely. Really thanking you from my heart. Please do me another favour by telling that is it a proper method to show categories, or there is any other easy method using the DataBinding method.

Regards

Kapalic

|||

Your code is fine. I would prefer to rely on the DataBoundItem value of the CurrentRow to get the selected category name. Here is some sample code:

Code Snippet

Dim dr As DataRowView

dr = dgvResults.CurrentRow.DataBoundItem

cboCategory.SelectedIndex = cboCategory.FindString(dr("CategoryName").ToString)

The benefit of this is to avoid the issues related to accessing the value of the cells directly because you might add new columns so your code will have to be changed. Better still, you should use the SelectedValue instead of the SelectedIndex but you will have to use the category ID not the name. In this case, you will have to include the category ID in your query for loading the grid but you shouldn't display it. You will have to handle the grid so that it only displays the fields you want.

Best regards,

Sami Samir

|||

Dear Sami

The second approach you suggest is better and easy. I've included the CategoryID in the query, and loaded in the grid. But problem now is can't hide the column! How to achieve this?

I use dgv.columns(3).Visible = False, but it produce an error!

Need another bit of help!

Kapalic

|||

I tried it and it worked. I'm not sure what the problem is. Your query for lading the datagrid should have 4 fields (SCategoryID, SCategoryName, CategoryID, CategoryName). Without attempting to hide the columns, the grid will display the 4 columns.

After making sure that the grid displays the 4 columns, add the line that hides the column, after completing all the code that loads the grid. It's still better to reference the column by its name and not its index.

Code Snippet

dgv.Columns("CategoryID").Visible = False

I'm not sure if I helped. If not, please post your code.

Best regards,

Sami Samir

|||

Dear Sami

Thank you very much for your reply. Surprisingly my previously posted code now working correctly. Maybe the problem was with the loading data or something else.

I want to really thank you again, because I've learned some very exclusive techniques which I did not know. It filled some big blanc space s in my knowledge which I required to work with relational database and ado.net. I am currently working on an inventory software which is my first project after learning vb2005. I would like to acknowledge to you in my software. I also would like to make friendship with you if possible.

Best Regards!

Kapalic

|||

Thanks for your kind words. This is the whole purpose of the forums which is helping others in overcoming any problems they might encounter. I am glad that I was able to assist you

Cheers,

Sami

No comments:

Post a Comment