Editable GridView Distinct Drop Down LIsts - c#

I have a simple app I am messing around with its a basic Master/Details layout.
The details panel is actually a tab panel and on one of the tab panels it has a GridView. Within that grid view it shows the "current" in database information with all cells as read only.
alt text http://lh4.ggpht.com/_JU1W2P96pD4/TAeonNNYXgI/AAAAAAAAAq0/Y_-Kse7VObE/ExampleA.jpg
I then have an add button that inserts a row into the GriView and allows the user to enter some information. The first item in my GridView is a DropDownList, which is populated from an ObjectDataSource. This drop down is basically my unique index and there can only be one selected value per GridView.
alt text http://lh3.ggpht.com/_JU1W2P96pD4/TAeonIF3DdI/AAAAAAAAAq4/JhfOTsHgsf8/ExampleWithDropDown.png
What is the best way to remove the values from the list that are already in the GridView? Do I just need to remove the data source and add a OnDataBinding method that iterates through the grid view and generates a valid list of values?
I can't use a service method because if the user adds two rows they would have the option to insert duplicates description types.
Just want to make sure there is not a better way to do this.

Keep the object datasource, which I am assuming is an IEnumerable object, in a Session variable. When someone selects a certain value remove that value from the list in the session variable. Whenever they activate the form where the drop down list is displayed simply rebind the control to the list in the session variable. It will be easier than looping through the grid every time the user adds an entry.

Related

How to pass selected list value from popup into a textbox using C# webforms?

I have a table called Vendor that has two columns: VendorNum & VendorName.
Is there a way to create a list or a listbox or a listview that when a button is clicked, this vendor list will pop up. Then the selected popup list's row will pass the selected VendorNum into a textbox that's in the parent page..
I only know GridView has the select row option, but is it a proper way to put the gridview into a Listbox so the list can be scrolled up/down all within a fixed box?
You might want to take a look at the ListBox.SelectValue and/or ListBox.SelectedItem properties

Best way to display data in a listbox from DB in C#

I have a local database, which holds records for cars, it has fields ID(ai, primary), plateNumber, Comments, ect..
Also I have a form with ListBox, button "Checked", and many text fields.
I am using winforms, and every time I run the application, constructor retrieves all the records from the db which have the bool field named "checked" == null, and displays the carnumbers in the Listview. When a specific plateNumber in that listBox is clicked, the other column data for the selected carPlate should appear in the textfields, but the
Problem is that there might be more records with the same plateNumber where "Checked" == null(true otherwise).
Question: how to keep the information about the specific plate number "behind the scenes" and when person clicks to select the platenumber from the listBox, it displays the data about the current(by id) platenumber in the form. Note that, if person clicks a "Checked" button on the form, it stores "TRUE" in the DB "checked" field and removes the specific carPlate from the listBox, and the next time application is launched it won't display that specific ID. In web I use hidden fields with javascript to achieve this, in C# I'm not aware how you keep information like that, I'm leaning towards arrays?
Thanks.
ListViews are populated with ListViewItems. The ListViewItem class has a Tag property, where you can store anything you want. Your "hidden" or "behind the scenes" data can be stored there.

Creating and accessing control values dynamically

I have a DropDownList that gets populated from SQL on page load.
A user will select an item from the list and click a button.
Based upon the item selected, I need to fetch data from SQL. The results are then used to dynamically create a table with textboxes (for user input).
After filling out some textboxes, the user will click another button. I will need to loop through the dynamically created table and do some data processing with the values entered in the textboxes.
I'm having a hard time knowing when to create the dynamic table. If on Page_Init, how do I get the DropDownList selected value to pass to SQL? If on Page_Load, how am I going to re-create the the table with the user-entered values?
This is in C# .NET Framework 4.0 using Webforms
Found the solution.
I'm using Request.Form[ddl.UniqueID] to get the DDL's selected value.

How to add a new row into datagridview using grid only. i.e by showing textboxes and add button in WinForm

I want to add a new row to a datagridview such that when a user clicks on new button , a new is is generated with few textboxes and combo boxes and after filling up all the details he save the info by clicking on save button.
EDIT
I want to do it like it is seen in gridview(Template Fields) in asp.net
I am looking for same kind of functionality.
Since I'm not sure exactly how you want to achieve this.. I would have a hidden panel with your text boxes, ...etc, show the panel when the new button is clicked. After all information is entered into the fields, click the save button. Assuming that you will be inserting this information into a table, after the row is inserted, call the stored procedure to get the desired records from the table being displayed in the grid.
Assuming your _dataGridView.Columns collection is not null and contains a template of the rows you wish to add, it is as simple as something like this:
foreach(var item in _collection)
{
_dataGridView.Rows.Add(item.Foo, item.Bar);
}
In order for this to work, you will have had to design your Columns collection in the VisualStudio designer or programatically add DataGridViewTextBoxColumn objects to the Columns collection.
In the example above, I added two DataGridViewTextBoxColumn objects to the _dataGridView.Columns collection and then populated the datagrid from a List of my object that contained a 'Foo' and a 'Bar'.
EDIT
Have you checked out the DataGridView FAQ? The information about using the DataGridView in unbound mode may help you.
HTH

Adding rows to a table based on user input (ASP.NEt)

I have a TextBox entry field where the user will enter a integer value. And then there is a "Create" button, which when clicked upon must generate a Table with 2 columns :
"Name" and "Email" being the column headers.
I want each row to have a textbox in each of these columns.
All of this has to happen after the button is clicked. I have discovered that if you dynamically add a control in ASP.NET(I am using C#) then the controls are lost during postback. And I don't know how to prevent that from happening.
Can somebody please give me some ideas regarding how to go about adding rows dynamically to a table (I tried using the asp.net Server side table control but ran into the "lost-during-postback" problem - can I try with something else like a gridview ? but afaik a GV will not work without data bound to it )
Point to note is that my table has textboxes for user entry and it is not for showing data ..rather it is for accepting data from the user which will be later used to persist details to the database.
Dynamic Controls
That's involved. Here's an interesting article on the issue of dynamic controls.
I think normally if you create dynamic controls then you're responsible for recreating them on postback; however the article contains a way to use the Init event if it's copacetic with your app.
Edit:
or Regular ASP.NET Controls
You can use data bound ASP.NET controls like: DataList, Repeater, GridView, etc. (You can put text boxes and all other kinds of controls into them for repeating in each row - or "item") and bind values to it. If you need to perform some processing on the user input first, then generate an internal Array, List, Dictionary, DataTable etc. (your choice) and bind that data as a data source to the ASP.NET control of choice.
aspnetControl.DataSource = myDataTable;
aspnetControl.DataBind();
or
aspnetControl.DataSourceId = "name_of_control";
There are various ways to assign a data source.
This way you won't run into the same problems as with dynamic control creation; however there is a lot to learn to facilitate this way too.

Categories

Resources