Solved

Bind DataGrid to OleDbAdapter

Posted on 2008-10-14
2
505 Views
Last Modified: 2012-05-05
I'm want to use the "Edit/Update" functionality of the ASP.Net DataGrid control.  I'm reading in the the data into using OledbDataAdapter from an Excel spreadsheet and binding to the DataGrid - and am then using the RowEditing and RowUpdating methods... but when I try to retrive the "New Values" from the DataGrid - the cells are empty strings.  What do I need to add in order to get this working properly?  

        //Create the OleDBAdapter from the Excel Spreadsheet
        OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Names$]", objConn);
     
        OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

        objAdapter1.SelectCommand = objCmdSelect;

        objDataset1 = new DataSet();

        // Fill the DataSet with the information from the worksheet.
        try
        {
            objAdapter1.Fill(objDataset1, "XLData");

        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("Error reading data" + ex.Message);
        }
                          
               
        GridView1.DataSource = objDataset1.Tables[0].DefaultView;
        Session["DataTable"] = objDataset1.Tables[0].DefaultView;
        GridView1.DataBind();


        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
         //Handle Grid Editing
         GridView1.EditIndex = e.NewEditIndex;
         GridView1.DataSource = Session["DataTable"];
         GridView1.DataBind();
       
         }
       
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

               
        string s1 = GridView1.Rows[e.RowIndex].Cells[1].Text;   ///This is an empty string

         foreach (object key in e.NewValues.Keys)
        {
            //No values in NewValues - even though change was made to grid cell          
             System.Diagnostics.Debug.WriteLine(key + ": " + e.NewValues[key]);  
        }
               
           

        string s = GridView1.Rows[e.RowIndex].Cells[0].Text;
               
        if (!String.IsNullOrEmpty(s))
        {
            id = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
        }
                   
         
        TextBox txtName = GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtName") as TextBox;
        string newname = txtName.Text;

        DataTable dt = Session["DataTable"] as DataTable;
        DataRow[] rows = dt.Select("First Name = " + GridView1.Rows[e.RowIndex].ToString());
        rows[0]["First Name"] = newname;
        GridView1.EditIndex = -1;
        GridView1.DataBind();
       
   }

0
Comment
Question by:coecho
2 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 22718045
Is this code written in the

if(!IsPostBack) event

like this code below?

if(!IsPostBack)
{
//Create the OleDBAdapter from the Excel Spreadsheet
        OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Names$]", objConn);
     
        OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
 
        objAdapter1.SelectCommand = objCmdSelect;
 
        objDataset1 = new DataSet();
 
        // Fill the DataSet with the information from the worksheet.
        try
        {
            objAdapter1.Fill(objDataset1, "XLData");
 
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("Error reading data" + ex.Message);
        }
                          
               
        GridView1.DataSource = objDataset1.Tables[0].DefaultView;
        Session["DataTable"] = objDataset1.Tables[0].DefaultView;
        GridView1.DataBind();
}

Open in new window

0
 

Accepted Solution

by:
coecho earned 0 total points
ID: 22844247
Couldn't get this to work.. Went with a different approach.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question