• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

c#, using a datagridview bound

I have used the datagridview control for years, but always unbound.  For the first time I set a datasource and don't fully understand what happened.  I see the DataSource for the grid is now "blahblahBindingSource".  Unfortunately, I don't know how to use it.

Is there a dataset associated with this?  When the user changes something in the grid, do I then need to update the dataset to commit changes to the database?

I am using .Net 4.5 if that makes any difference.  I am connected to a SQL Server database.

Any help would be greatly appreciated.
  • 3
  • 2
1 Solution
Hello g_johnson,  
it appears you are now doing things the easy way, by setting the datasource from the markup page rather than the behind code.  

You generally do not need any code behind (ie C#) to support this method of binding. Just a data source eg SQLDataSource (if using MSSQL), which allows you to reference the table or view that you need to display.  It allows you to construct the query or display the table by simply selecting the fields you require.

In your datagrid you then bind it to that data source using the smart tags.  This is easy to do.  It may then create some code on the markup page which helps to display the fields based on their type, eg int, text etc.  It is then very easy to make changes to your displayed grid, like change displayed field names atc.

This is generally easier to do and is great for testing and mock-ups but can lead to some security risks if people view the source code of the page, and may see the connection string and data binding information, eg field names etc.

hope this clears things up 4 u

here is a sample in action

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Rulesdb2]"></asp:SqlDataSource>
        <br />
        Sample Grid Here<br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Rulesdb2ID" DataSourceID="SqlDataSource1" EnableModelValidation="True">
                <asp:BoundField DataField="Rulesdb2ID" HeaderText="Rulesdb2ID" ReadOnly="True" SortExpression="Rulesdb2ID" />
                <asp:BoundField DataField="Rulesdb2Text" HeaderText="Rulesdb2Text" SortExpression="Rulesdb2Text" />

Open in new window

g_johnsonAuthor Commented:
This is a Windows Form application, the source code will not be visible to the user so that's good -- no security issues.  However, when the user changes something in the grid, how does that get committed to the database?

g_johnsonAuthor Commented:
I found the answer to the question.  You choose the appropriate variation of the dataAdapter.Update method, such as:


Thanks for your input.
if you plan to make an web application the code above will work fine also.
g_johnsonAuthor Commented:
This answers the question directly and completely.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now