?
Solved

Bind DataGrid to OleDbAdapter

Posted on 2008-10-14
2
Medium Priority
?
523 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

649 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