0

I have a C# Winforms app that is connecting to a SQL Server 2005 database. The form I am currently working on allows a user to enter a large amount of different types of data into various textboxes, comboboxes, and a DataGridView to insert into the database. It all represents one particular type of machine, and the data is spread out over about nine tables.

The problem I have is that my DataGridView represents a type of data that may or may not be added to the database. Thus, when the DataGridView is created, it is empty and not databound, and so data cannot be entered. My question is, should I create the table with hard-coded field names representing the way that the data looks in the database, or is there a way to simply have the column names populate with no data so that the user can enter it if they like? I don't like the idea of hard-coding them in case there is a change in the database schema, but I'm not sure how else to deal with this problem.

2
  • What do you mean "may or may not be added to the database?" Is it populated with data coming from a table? Is the grid going to allow users to update/insert/delete data from said table? Commented May 3, 2010 at 18:14
  • The form is strictly for performing an INSERT query, so the DataGridView starts off empty and not databound. It doesn't display any existing data; it's meant to add data, but is entirely optional. Commented May 3, 2010 at 20:30

1 Answer 1

1

You could query the database for the names of the columns, if you're using a SqlDataReader you can use it's GetSchemaTable. Here's some sample code for doing this:

http://support.microsoft.com/kb/310107

Sign up to request clarification or add additional context in comments.

1 Comment

Great! That's just what I needed. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.