Advertisement

08.26.2007 at 07:07PM PDT, ID: 22788334
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Cannot Update a database via DataGridView Control after programatically adding Columns - C#

Tags: datagridview, update, database
Hello,

1. I have a Windows Form app where I am binding a DagridView to a Dataset which was filled from a SQL Server database table.

2. I then have a method to Update the database.

This all works fine but I wanted to add the columns to the DataGridView programatically in order to customize them.

Using Jim Brandleys code example, I am able to do this, add the columns programatically and view all of the data from the SQL table in my DataGridView and customize columns too.

3. Problem: My Update method no longer works. See code below&

I removed the lines to bind the dataGridView to the datSet because the compiler complain when I was programatically adding columns

//dataGridView1.DataSource = dataSet1;
//dataGridView1.DataMember = "Stats";  

I suspect that I am not properly bound to the dataSet (and SQL table) the way I am trying to update but cant see it..                    



SQL = SELECT A,B,C  FROM SQLTable
Conn = new OleDbConnection(ConStr))
cmd = new OleDbCommand(SQL, Conn)
Conn.Open();
da = new OleDbDataAdapter()
da.SelectCommand = cmd;
oleDbCb = new OleDbCommandBuilder(da)
dataSet1.Clear();
da.Fill(dataSet1, "Stats");

//note removed these two lines in order to programatically add Columns to DataGridView
//dataGridView1.DataSource = dataSet1;
//dataGridView1.DataMember = "Stats";                      

//set DataGrid Properties
dataGridView1.AutoGenerateColumns = false;
//etc
//etc                      
//call Jims AddColumns method to add columns to  attach columns to the DataGrid
 AddColumns();
                       
 //fill the grid with Object Array - The indexer is the number of columns
 Object[] cellData = new Object[4];
 DataTable dtble = dataSet1.Tables["Stats"];
 foreach (DataRow row in dtble.Rows)
 {
  //celldata adds a column, row.ItemArray adds all rows of that column
  cellData[0] = row.ItemArray[0];
  cellData[1] = row.ItemArray[1];
  dataGridView1.Rows.Add(cellData);
 }//end foreach

//Jims AddColumns() Method:
// Text Box Column only for numeric values, of type int

AddColumns
{
DataGridViewTextBoxColumn column2 = new DataGridViewTextBoxColumn();
//DataGridViewTextBoxColumn column3instance created in //Declarations
            column2.Name = "ID Number";
            column2.HeaderText = "ID Number";
            column2.Width = 48;
            column2.ValueType = typeof(int);
            cell = new DataGridViewTextBoxCell();
            column2.CellTemplate = cell;
            this.dataGridView1.Columns.Add(column2);

}//end AddColumns()

My Update() Method (This does not work now that I am programatically adding Columns to the DataGridView)

UpdateDataBase()
{
SQL = SELECT A,B,C  FROM SQLTable
OleDbConnection Conn = new OleDbConnection(ConStr);
//OleDbDataAdapter da = new OleDbDataAdapter();//in Declarations
OleDbCommandBuilder oleDbCb = new OleDbCommandBuilder(da);
Conn.Open();
OleDbTransaction trans = Conn.BeginTransaction();
cmd = new OleDbCommand(SQL, Connn)
da.SelectCommand = cmd;
cmd.Transaction = trans;
da.Update(dataSet1, "Stats");
MessageBox.Show("Your changes have persisted to the Database!");
trans.Commit();

Can anyone see what I am missing here?

Thanks
Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: Saxitalis
Solution Provided By: JimBrandley
Participating Experts: 2
Solution Grade: B
Views: 309
Translate:
Loading Advertisement...
08.27.2007 at 05:37AM PDT, ID: 19775017

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.28.2007 at 01:37AM PDT, ID: 19781242

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.28.2007 at 09:49AM PDT, ID: 19784775

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.28.2007 at 10:06PM PDT, ID: 19788846

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.29.2007 at 08:18AM PDT, ID: 19792149

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.14.2007 at 06:11AM PDT, ID: 19891071

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080236-EE-VQP-29 / EE_QW_1_20070628