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(objDatase t1, "XLData");
}
catch (Exception ex)
{
System.Diagnostics.Debug.W riteLine(" Error reading data" + ex.Message);
}
GridView1.DataSource = objDataset1.Tables[0].Defa ultView;
Session["DataTable"] = objDataset1.Tables[0].Defa ultView;
GridView1.DataBind();
protected void GridView1_RowEditing(objec t sender, GridViewEditEventArgs e)
{
//Handle Grid Editing
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = Session["DataTable"];
GridView1.DataBind();
}
protected void GridView1_RowUpdating(obje ct 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.W riteLine(k ey + ": " + 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]. FindContro l("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();
}
//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(objDatase
}
catch (Exception ex)
{
System.Diagnostics.Debug.W
}
GridView1.DataSource = objDataset1.Tables[0].Defa
Session["DataTable"] = objDataset1.Tables[0].Defa
GridView1.DataBind();
protected void GridView1_RowEditing(objec
{
//Handle Grid Editing
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = Session["DataTable"];
GridView1.DataBind();
}
protected void GridView1_RowUpdating(obje
{
string s1 = GridView1.Rows[e.RowIndex]
foreach (object key in e.NewValues.Keys)
{
//No values in NewValues - even though change was made to grid cell
System.Diagnostics.Debug.W
}
string s = GridView1.Rows[e.RowIndex]
if (!String.IsNullOrEmpty(s))
{
id = int.Parse(GridView1.Rows[e
}
TextBox txtName = GridView1.Rows[e.RowIndex]
string newname = txtName.Text;
DataTable dt = Session["DataTable"] as DataTable;
DataRow[] rows = dt.Select("First Name = " + GridView1.Rows[e.RowIndex]
rows[0]["First Name"] = newname;
GridView1.EditIndex = -1;
GridView1.DataBind();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if(!IsPostBack) event
like this code below?
Open in new window