Solved

Bind DataGrid to OleDbAdapter

Posted on 2008-10-14
2
515 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Set custom font WPF 2 21
Header Font Size in Grid View 6 26
Need help with a query 14 39
C# MDI - Use Button on Child Form to Open (or Show) a Different Child Form 3 23
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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