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;
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;
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.