This is how i have it setup:
DAL: a dbml file for the context
BLL: All my BL rules
Types: I made type for every object that i return to my presentation layer.
Now that i made types and use this structure i find it hard to understand how to manipulate data. Normally i could just bind my datacontext to a grid and just activate all the datagrid's possibilities without having to worry about anything.
Now for example when i want to enable sorting, i have to write my own logic to return teh right rows to the grid etc.
I have a multiview with two datagrids. The first datagrid is an overview grid and has an option to select an entry. When i select the entry i switch to my second view and I want to show the details for the selected object there.
However... I bound a List to the first grid and now I don't know how to find out what entry is selected. I have an event handler for the SelectedIndexChanged.
I want to catch the right details for the 2nd datagrid there.
assuming that your objects from the BLL have an Id property, apply a the name of the property as a datakey to the first GridView. In the aspx page, add the following property to the gridview
DataKeyNames="Id"
On the SelectedIndexChanged, get the Id of your selected object using the following code:
string id = myGrid.SelectedDataKey.Value;
Now that you have your Id, you can query your needed object from the DB.
Related
I'm loading a record to an aspx page. I have two comboxes. (AJAX comboboxes in this case) The second Loads based on the id from the first.
When I pass a key to the page in the query string I retrieve data into a class, then I populate the page fields from the class. When I set the carrier.selectedvalue = class.1value the selection shows correctly. When I set product.selectedvalue = class.2value the selection does not take. (The product has nothing selected)
If I drop down the list of the product combobox the correct data is loaded based on the the first combox (carrier).
I have tried two methods:
1) query product sql dataset (2nd dataset) based on selected value from the carrier combox
2) Load ALL products (2nd dataset) then FILTER the products based on the value from the carrier combox
Both methods load the 2nd combox with the values I need. Neither method helps me get the product combobox to show the selected value once the page renders.
Note that I'm doing all this in page_load
-Thanks in advance for looking.
The problem is that you are doing everything in Page_Load. Do things in the following order in the following events to prevent your issue:
Page_Init: Populate Carrier Items
Page_Init: Set Carrier selected value
Page_Load: Clear Items Product.Items.Clear()
Page_Load: Populate Product Items
Page_Load: Set Product Selected Items
See this guide for how to effectively use the Page events: http://attemptsatprogramming.blogspot.com/2011/03/practical-guide-to-aspnet-event-model.html
Gthompson83 put me on the right track. It was a databinding issue. I moved the set of the product combobx (the second combobox) to the databound event like so:
protected void cboProduct_DataBound(object sender, EventArgs e)
{
// Set the Product cbo
cboProduct.SelectedValue = c.Product_ID.ToString();
}
That is all it took. The class is still populated in the Page_Load event based on values from the query string. Once the DataBound event is fired i use the values I've placed in the class to set the value of the product combobox.
Some helpful info is here: Databinding events for data-bound controls
On the page linked above there is a good section on "Nested Data-Bound Controls".
Note that I did not follow that example completely... I did not do the databind on the second combobox programmatically... just catching the DataBound event was enough to allow me to set the selectedvalue.
I use a DataGridView to view and edit data in my PostgreSQL database, which works fine. I want to make it a little more user-friendly by choosing the right Control to input data. Specifically, I want to create a ComboBox in a column to let the user search and select a value fast.
To do so, I think I need the EditingControlShowing event to fill the combobox. However, the column I get is of type DataGridViewTextBoxColumn, so the corresponding e.Control is a TextBox instead of a ComboBox.
I never initialise those columns, because they come from views in the database. How can I cast/initialise the column to DataGridViewComboBoxColumn ?
This is how I populate my DataGridView:
dgView.DataSource = getView();
getView() returns a DataTable as can be gotten from NpgsqlDataAdaper.
Since you want to use something other than a DataGridViewTextBoxColumn you need to set AutoGenerateColumns to false and actually define the columns yourself. When defining them, choose a DataGridViewComboBox column for the appropriate fields.
This can be done in code, or in the designer, and based off your question I think you should be able to simply do it through the designer.
You should do it in 2 steps.
1. First of all ( as mentioned by Michael) you should set AutoGenerateColumns to false and then create the required columns using the datagrid view columns option (in properties)
2. Then you should loop through your records and assign appropriate values to appropriate columns.
In case you have hundreds of records that you are showing in grid view, than you should consider using server side paging.
In page_load, I create a table and fill it with data gathered from a database. Then after allowing the user to modify it, I need to make the changes to the database. But I'm not sure how to do this. I have been looking around and now am more confused then when I started. My code to create the table looks like this:
delList.Controls.Clear();
Table tbl = new Table();
tbl.ID = "tbl1";
tbl.BorderWidth = 1;
delList.Controls.Add(tbl);
This code gets added to a placeholder:
<asp:PlaceHolder ID="delList" runat="server"></asp:PlaceHolder>
I want to have a button at the bottom of the page to allow the user to select in order to save the changes but I can't figure out how to do this. Mostly because there will be a lot of rows that need to be updated. I was thinking it might be easiest to parse through the table using javascript and then somehow calling a function to make update the database one row at a time. But I'm not sure if this is even possible.
All help will be greatly appreciated.
It would be a lot easier if you used a control that is specific to this kind of operation. I mean a control like GridView.
GridView displays the values of a data
source in a table where each column
represents a field and each row
represents a record. The GridView
control enables you to select, sort,
and edit these items.
The GridView control is used to
display the values of a data source in
a table. Each column represents a
field, while each row represents a
record. The GridView control supports
the following features:
Binding to data source controls, such as SqlDataSource.
Built-in sort capabilities.
Built-in update and delete capabilities.
Built-in paging capabilities.
Built-in row selection capabilities.
Programmatic access to the GridView object model to dynamically set properties, handle events, and so on.
Multiple key fields.
Multiple data fields for the hyperlink columns.
Customizable appearance through themes and styles.
i dont get how the User Can modify content in a Table, unless you have some form Controls like Textbox, Dropdown list?
You should be creating form fields dynamically, and adding it to a container/form on the Page along with a button (which could do a postback/ or handle JavaScript).
You Could iterate through all controls in a parent container or add custom attributes to your form elements, do that you can identify those fields in JS/Clientside.
I have a grid with three columns, two of which contain drop-downs, all of them getting filled from a web service result set. Now I want to allow the functionality of adding a new record in the grid by clicking an Add button present outside the gridview.
Whenever the user clicks the Add button, a new record should be created in the grid with value list filled in the drop-downs with the available options.
What is the best possible way to achieve this considering extensibility in mind.
Thanks a lot!
P.S. The data source set for the grid is a list.
Add a blank item to the list, and rebind with this new list and dummy item. That's typically one way to do it, or store the insert form in the footer of the columns. I've used that approach.
I have a GridView, I bind it to ObjectDataSource. ObjectDataSource takes data from a database's table. Each row in table has unique ID.
I have a button in each row of GridView that should remove this row from database. My ObjectDataSource returns Object, this returned object contains ID (and some other information, like name, user etc.) however I do not show this ID on my grid.
Question how can I get these ID after user chooses to delete row, I need to know what I should remove.
You should assign DataKeyName property of the grid view, once you do that, you can get value of your id that you have provided in .DataKey property, which is explain in detail here with source code
If you using BindingSource, than you always can get current object.
For example somewhere in mouse click handle:
var myData = (MyData)bindingSource.Current;
MyDataRepository.DeleteMyDataById(myData.Id);