如何在特定Datagridview列中加载Excel特定列值 [英] How Do I Load Excel Specific Column Value In Specific Datagridview Column

查看:116
本文介绍了如何在特定Datagridview列中加载Excel特定列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要你的帮助。我创建一个应用程序,其中数据从excel文件加载到datagrid视图,但它也加载列名称我只想加载值到指定列

Hi
I need your help. i make an app in which data load from excel file into datagrid view but it also load column name i just want to load values to specif column

try
{
    string pathcon = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + textBox_path.Text + "; Extended Properties = 'Excel 8.0;HDR=yes;IMEX=1'";
    OleDbConnection conn = new OleDbConnection(pathcon);
    OleDbDataAdapter myadapter = new OleDbDataAdapter("select Clients from[" + textBox_sheet_no.Text + "$A2]", conn);
    DataTable dt = new DataTable();
    myadapter.Fill(dt);
    dataGridView_VAT.DataSource = dt;
}
catch (Exception e1)
{
    MessageBox.Show("Error in Connection " + e1.Message);
}

//this is my code but i want to show like
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dataGridView_VAT);
row.Cells[0].Value = 
row.Cells[1].Value = 
row.Cells[2].Value = 
row.Cells[3].Value =
row.Cells[4].Value = 
row.Cells[5].Value = 
dataGridView_VAT.Rows.Add(row);







已添加 pre 标签。

[/编辑]

推荐答案

A2] ,conn);
DataTable dt = new DataTable();
myadapter.Fill(dt);
dataGridView_VAT.DataSource = dt;
}
catch (例外e1)
{
MessageBox.Show( 连接错误 + e1.Message);
}

// 这是我的代码,但我希望显示为
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dataGridView_VAT);
row.Cells [ 0 ]。值=
row.Cells [ 1 ] .Value =
row.Cells [ 2 ]。值=
row.Cells [ 3 ]。值=
row.Cells [ 4 ]。值=
row.Cells [ 5 ]。值=
dataGridView_VAT.Rows.Add(行);
A2]", conn); DataTable dt = new DataTable(); myadapter.Fill(dt); dataGridView_VAT.DataSource = dt; } catch (Exception e1) { MessageBox.Show("Error in Connection " + e1.Message); } //this is my code but i want to show like DataGridViewRow row = new DataGridViewRow(); row.CreateCells(dataGridView_VAT); row.Cells[0].Value = row.Cells[1].Value = row.Cells[2].Value = row.Cells[3].Value = row.Cells[4].Value = row.Cells[5].Value = dataGridView_VAT.Rows.Add(row);







已添加 pre 标签。

[/编辑]


如果我正确理解你想要的东西,这应该是一个解决方案。



1.创建预定义列。

If I correctly understand what you want, this should be a solution for you.

1. Create predefined columns.
private System.Windows.Forms.DataGridViewTextBoxColumn column1;
private System.Windows.Forms.DataGridViewTextBoxColumn column2;





2.将 DataPropertyName 设置为Excel列名。



2. Set the DataPropertyName to the Excel column name.

this.column1.DataPropertyName = "ExcelCol1";
this.column1.HeaderText = "Col1";
this.column1.Name = "column1";
this.column1.Width = 50;

this.column2.DataPropertyName = "ExcelCol2";
this.column2.HeaderText = "Col2";
this.column2.Name = "column2";
this.column2.Width = 50;





3.将列添加到 DataGridView



3. Add the columns to the DataGridView

this.dataGridView_VAT.Columns.AddRange(
new System.Windows.Forms.DataGridViewColumn[] {
            this.column1,
            this.column2});





现在,当您使用值填充 DataTable 时,它们将落入右列并且您可以根据需要对列重新排序,因为列的名称是键,而不是索引。



Now when you fill the DataTable with values they will fall into the right columns and you can reorder the columns as you please, as the name of the column is the key, not the index.


这篇关于如何在特定Datagridview列中加载Excel特定列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆