开发者

data not getting displayed in datagridview

i am currently working on importing data from excel sheet to sql server.i have written a code for removing empty rows from the excel sheet..now i am trying to see the data in datagridview by assigning the method name to the datagridview datasource..but it is not displaying any data.Can anyone say me if i am doing correct or wrong. This is my code..

   public partial class Form9 : Form
{
    DataSet dsExcel = new DataSet();

    public Form9()
    {
        InitializeComponent();
    }

    private void btnUpload_Click(object sender, EventArgs e)
    {
        //String input = string.Empty;
        OpenFileDialog filepath = new OpenFileDialog();
        filepath.Filter = "Excel Files(*.xls)|*.xls|Excel Files(*.xlsx)|*.xlsx|Text Files(*.txt)|*.txt|All Files(*.*)|*.*";
        filepath.InitialDirectory = "C:";
        filepath.Title = "Select a file";
        if (filepath.ShowDialog() == DialogResult.OK)
            txtExcelFile.Text = filepath.FileName;
        if (txtExcelFile.Text == string.Empty)
            return;
    }

    private void btnImport_Click(object sender, EventArgs e)
    {


        OleDbConnection cnn = new OleDbConnection(@"Provide开发者_JAVA技巧r=Microsoft.Jet.OLEDB.4.0;Data Source='" + txtExcelFile.Text + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'");
        cnn.Open();
        try
        {

            OleDbDataAdapter data = new OleDbDataAdapter("select * from [Customers$]", cnn);

            data.Fill(dsExcel);

            dgvCustomers.ColumnHeadersVisible = false;
            dgvCustomers.DataSource = GetExcelData();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            //cnn.Close();
        }

    }

       public DataSet GetExcelData()
        {
        DataTable ExcelTable = new DataTable();
        List<int> rowToRemove = new List<int>();
        //DataRow ExcelRow = new DataRow();

        foreach (DataRow excelrow in dsExcel.Tables[0].Rows)
        {
            bool IsEmpty = false;
            foreach (object item in excelrow.ItemArray)
            {
                if (String.IsNullOrEmpty(item.ToString())) 
                {
                     IsEmpty = true;
                     break;

                }                  
                else
                {
                    IsEmpty = false;

                }
            }
            if (IsEmpty)
            { 
                rowToRemove.Add((dsExcel.Tables[0].Rows.IndexOf(excelrow)));
            }

        }

        for (int i = rowToRemove.Count - 1; i >= 0; i--)
        {
            dsExcel.Tables[0].Rows.RemoveAt(rowToRemove[i]);

        }
        return dsExcel;

    }
}

}


Your datasource should be a datatable in the dataset, not the dataset itself.

Change

public DataSet GetExcelData()

to

public DataTable GetExcelData()

and at the end, return this:

return dsExcel.Tables[0];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜