Retrieve SQL data on dynamic created textbox - c#

I am using C#, Visual Studio 2005
I have created textbox on runtime/dynamic way and inserted/saved sql data on database but the problem is how to retrieve the same data on same way for edit / update.
e.g.
col1 col2
mahesh 1000
kirti 2000
The above data is stored in sql server. I don't know how to retrieve the same on dynamic way by creating dynamic textbox and retrieve the same.
Suggest proper code please.

Create datasource. Read the data into the datasource. Add the controls and bind the dynamic controls to that data source.

I'm not quite sure i follow 100% but you could have a drop down that is populated from the database, Select the item in this drop down, Enter the details in to your textboxes and have a button to activate the update.
In steps
Get your data into a local list.
Populate the drop down using this list.
You now still have your local list with the appropriate data.
Get the user to select an item from the drop down.
Use the drop downs selected index to get the item from the list.
Change the values within this item and commit to the database
I would have thought it would be far easier using the appropriate control for displaying the information such as a listview or datagrid
List<String> objInformationList = new List<String>();
objInformationList = "Your database query function";
foreach(String objCurrentString in objInformationList){
// Create your text box
// Set your text within your textbox
// Add the text box to the screen or a table structure
}
I personally would be doing this another way

Related

How do I set the content of a label to a database table cell in WPF?

I have built a program in WPF using Visual Basic that allows users to select from a series of items. The list of items may change as I add more items or remove older ones. Originally, I would just add and remove these items in the project code, and rebuild the executable.
Over time, however, the list of items grew and became tedious and impractical to maintain in the code. It occurred to me that it would be easier to store the items in a database table which the program can read, and the list can just be updated by adding and removing items from this database.
I have the database built, and I've added it as a Data Source. I have the data connection set up and everything is ready to go.
What I need to know is how to set the Content property of a label to a cell from a table in the database. I want to do this in the codebehind, not in markup (XAML).
My guess is that I will need to set up a sort of query with a for loop to find the cell I want.
The name of the database is ItemsDB. It contains a table called ItemsTable, and the fields in the table have a unique ID (key) with an AutoNumber data type. The column containing the data I want is named ItemLabel.
So, to sum it all up, I want a label to show the content from a single cell in the database, specified in the codebehind. Either C# or VB is fine.
EDIT
I guess I should've mentioned that the labels themselves actually function as buttons, and I don't really want to display a ListView if I don't have to. I know it's not helpful to not have any code posted, but I don't even know where to start, so there's no code to post.
Took a while longer than I expected, hope this is still useful. So, what you want to do is iterate through a collection and create a new Label for each row.
You will need a container for the Labels, I used WrapPanel, but you can use any Panel you want. I'm replacing your Data Source with a simple DataTable, you'll get the point from this:
foreach (DataRow row in YourItemsTable.Rows)
{
Label newLabel = new Label();
newLabel.Content = row["ItemLabel"].ToString();
// If you need some events, attach the handlers here
yourParentContainerPanel.Children.Add(newLabel);
}
This should be all you need.

Is two data sources for a DataGridView.ComboBoxColumn possible?

I have a DataGridView with several TextBoxColumns and one ComboBox column called 'combo' that holds the client type. The problem is that I'd like to show both the currently selected client-type value along with the dropdown client-type list to validate future changes by the user. In SQL Server, I have a DB with two table columns, 'client_type_dropdown.name' and 'clients.client_type'. The 'client_type_dropdown.name' column is a validation list. The 'clients.client_type' column contains the current client type for clients in the database. Is there a way to show in 'combo' both 'client_type_dropdown.name' and 'clients.client_type' , i.e., one source for the ComboBoxColumn dropdown and a different source for the textbox part of 'combo'? Or do I need to have two columns in my grid?
I appreciate your help.
I'm using a third party grid, but I usually handle this by setting the combo drop down style to DropDown instead of DropDownList. This will allow your original database value to display, even if it isn't in the list.
This also allows free typing of values into the combo field, so the trick after that is to validate the user input to make sure it matches a value in the list before you allow them to save updated values. You could play around with the LimitToList property of the combo to possibly save you doing the validation manually, but with most controls I have worked with it will give you more grief than help.

Using Radgridview directly for data Manipulation

So i am using RadGridView for Winforms.
Now what i want to do is to perform data manipulation in the radgridview and datasource to get updated after the row loses foucs. I don't want to use textboxes or any other control for entering data.
Something like this
And after i press the new add new row and enter data
Now this data isn't being updated in the dataSource to update i need a query that needs to executed against the datasource.
Now i haven't been able to build a query because i haven't been able to figure out how to access the data of the newly created cells so that i can build a query and pass to SQLCeDataAdapter for execution.
Any pointers will be appreciated.
Im not sure about RadGridView, but I use DataGridView, wich I guess works the same way
You need to get the datarow so u can update to the database
DataRow row = ((DataRowView)eTipoDocumentoBindingSource.Current).Row;
once you have it, you can use the DataTableAdapter method update(row) to save changes to the database
You can do that, either in the click event of a save button, or in the event of focused row changed

Binding a listbox/datagrid view manually with Data Table Vs binding it with Data Sets.Data Table[0] in c# winForms

There are two ways in c# that I can bind my listbox with a datasource:
1) Query the SQL Server 2008 db and the query will return a Data Table which can use it to bind it to list box.
2) Create a new Data Set by right-clicking in the solution explorer and going through the wizard to complete it and finally calling the dataset.fill method to fill the data table which I can bind it to my list box.
I have a list box which displays the names of forms that I have created in C#. Basically this is used to grant which user has right on which form.
My question is which method should I go for? Currently I'm using method 1. Same question also goes for a data grid view.
It dosn't make any difference.
DataSet.Tables[integer] is a DataTable

Inserting new row in gridview using an external Add button and a list as data source

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.

Categories