Search code examples
c#asp.netgridviewdatatable

Bind data table to Grid view but the Row Edit Event not working in gridview


I am making an importer which will initially upload excel to a folder then I am going to show(i am gonna put that data after validation into a data table and show it to the user for any updation or deletion) that data to a user. The user will verify that everything seems fine and then click on save button and data will be saved to a database table. Now I have questions in my mind.

  1. Is it a good idea or approach to first put data in the data table and show it to the user for updating or deletion.

  2. secondly I am showing data in grid view so that the user will edit or delete any row in a data table.

  3. I have a problem when I click on edit link first time it does nothing. Actually I have to click twice on the edit link then grid view go into edit mode. I have googled this problem and they say I have to rebind the grid view with the data source. Now the problem is when I rebind it on row editing event at that time data table object is null. so what to do. I have pasted my code below.

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Web;
     using System.Web.UI;
     using System.Web.UI.WebControls;
     using System.Data;
     using System.IO;
     using System.Data.SqlClient;
     using System.Data.OleDb;
    
     public partial class ImportExcelToDatabase : System.Web.UI.Page{
     DataSet ds;
     DataTable Dt;
     protected void Page_Load(object sender, EventArgs e)
     {
    
     }
     protected void btnIpload_Click(object sender, EventArgs e)
     {
         ImporttoDatatable();
    
     }
     private void ImporttoDatatable()
     {
         try
         {
             if (FlUploadcsv.HasFile)
             {
                 string FileName = FlUploadcsv.FileName;
                 string path = string.Concat(Server.MapPath("~/Document/" + FlUploadcsv.FileName));
    
                 FlUploadcsv.PostedFile.SaveAs(path);
    
                 OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");
    
                 OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", OleDbcon);
                 OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
                 ds = new DataSet();
                 //Dt.Clear(); ds.Clear();
                 objAdapter1.Fill(ds);                
                 Dt = ds.Tables[0];
                 DataColumn dc = Dt.Columns.Add("Ser", typeof(Int32));                
                 int count = 0;
                 foreach (DataRow item in Dt.Rows)
                 {
                     count++;
                     item["Ser"] = count;
                 }
    
                 gvEmployee.DataSource = Dt;
                 gvEmployee.DataBind();
             }
         }
         catch (Exception ex)
         {
    
         }
     }
    
     protected void gvEmployee_RowUpdating(object sender, GridViewUpdateEventArgs e)
     {
    
     }
     protected void gvEmployee_RowEditing(object sender, GridViewEditEventArgs e)
     {
         gvEmployee.EditIndex = e.NewEditIndex;
         gvEmployee.DataSource = Dt;
         gvEmployee.DataBind();
    
     }
     protected void gvEmployee_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
     {
         gvEmployee.EditIndex = -1;
         gvEmployee.DataSource = Dt;
         gvEmployee.DataBind();
    
     }
    

    }


Solution

  • My problem was solved by just tweaking the process as per requirement. Now I just read the data from excel file and validate it like data type of the column should be correct so there should be no undefined value and then insert it into the database table and if there is any validation error I am showing user a message that Wrong input in the cell # of excel file.