Solved

Bind DataGrid to OleDbAdapter

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Default parameter problem in C# 3 27
ASP.NET Web Form Variables being reset 2 18
Chat Room 1 30
XML & .net 5 21
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now