Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Excel sheet as follows

StudentName   ParentName  MobileNo      Eng  Tam  Mat Sci Social

  Ram          Suresh     9789502560     70   60  AB   60  65
  Sam          Sudhir     9940560520     60   55  75   AB  70
  Vijay        Ajay       9045078505     78   AB  60   50  AB


Run mode as follows;

Browse (Button)

When i click the Browse (Button) the above Excel sheet will be displayed in datagridview as follows;


In Datagridview as follows;

StudentName   ParentName  MobileNo      Eng  Tam  Mat     Sci Social

    Ram          Suresh     9789502560   70   60          60  65
    Sam          Sudhir     9940560520   60   55  75          70
    Vijay        Ajay       9045078505   78       60      50             

But in the Gridview AB(Absent) is not displayed in the Datagridview.


Browse Button code as follows;

C#
 private void Browse _Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult MsgDr = MessageBox.Show("Excel file format should be in Student Name,Parent Name,Mobile No,Marks. \nAre you sure want to continue?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (MsgDr == DialogResult.No)
                    return;

                LblSheetName.Text = "";
                OpenFileDialog fldg = new OpenFileDialog();
                fldg.Title = "Select File";
                fldg.FileName = txtFileName.Text;
                fldg.Filter = "Excel Sheet(*.xls)|*.xls|Excel-2007 Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
                fldg.FilterIndex = 1;
                fldg.RestoreDirectory = true; //added
                if (fldg.ShowDialog() == DialogResult.OK)
                {
                    txtFileName.Text = fldg.FileName;
                    FileInfo file = new FileInfo(txtFileName.Text);
                    if(IsFileLocked(file) == true) 
                    {
                        MessageBox.Show("Selected file is opened already/not properly closed/using by some other person. Close it properly and try again", "File already opened", MessageBoxButtons.OK);
                        txtFileName.Text = string.Empty;
                        return;
                    }     
                    Import();
                    System.Windows.Forms.Application.DoEvents();
                }
            }
            catch (Exception ex1)
            {
                progressBar1.Visible = false;
                MessageBox.Show("Error in open" + ex1.ToString(), "Error", MessageBoxButtons.OK);
                return;
            }
}



  private void Import()
        {
            try
            {
                progressBar1.Visible = true;
                progressBar1.Value = 700;

                String Pathname = txtFileName.Text;
                OleDbConnection conn = new OleDbConnection();
                conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Pathname + ";Extended Properties=\"Excel 8.0;HDR=YES\";";
                conn.Open();

                GSheetName = "";
                System.Data.DataTable dt = conn.GetSchema("Tables");
                string sheetname = "";
                foreach (DataRow row in dt.Rows)
                {
                    sheetname = (string)row["TABLE_NAME"];
                    if (sheetname.EndsWith("$"))
                    {
                        GSheetName = sheetname.ToString().Trim();
                        break;
                    }
                }
                LblSheetName.Text = "Sheet Name :" + GSheetName.ToString().Trim();

                if (GSheetName == "" || GSheetName == null)
                {
                    progressBar1.Visible = false;
                    MessageBox.Show("Invalid Sheet Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                OleDbCommand command = new OleDbCommand("SELECT * From[" + GSheetName + "]", conn);
                DataSet ds = new DataSet();
                OleDbDataAdapter adapter = new OleDbDataAdapter(command);
                adapter.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
                progressBar1.Visible = false;
            }
            catch (Exception ex)
            {
                progressBar1.Visible = false;
                MessageBox.Show("Error in Import" + ex.ToString(), "Error", MessageBoxButtons.OK);
                return;
            }
        }

how to show the Alphabets while importing the excel sheet into datagridview.
please help me what si the problem in my above code.

Regards,
Narasiman P.
Posted
Updated 3-Dec-13 10:40am
v2
Comments
Maciej Los 3-Dec-13 16:42pm    
What you mean: "how to show the Alphabets"? Where? When?
TnTinMn 3-Dec-13 21:21pm    
He means letters. The columns contain both numeric and string data.
TnTinMn 3-Dec-13 21:19pm    
Try add IMEX=1 to the Extended Properties part of your connection string. That tells it that your columns can have mixed data types; i.e. String and Integer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900