Link to home
Start Free TrialLog in
Avatar of coecho
coecho

asked on

Bind DataGrid to OleDbAdapter

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();
       
   }

Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

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

ASKER CERTIFIED SOLUTION
Avatar of coecho
coecho

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial