在C#.net的Datagridview中更改标题名称 [英] Changing the Header name in Datagridview in C#.net
本文介绍了在C#.net的Datagridview中更改标题名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经上传了一个Excel文件并在Datagridview C#中进行了查看.我的查询是在运行时在标头中,我像这样获取F1,F2,F3 ...如何获取列名而不是获取F1,F2,F3. ..
问候
Balamurugan
Hi,
I have Uploaded a Excel file and viewed in Datagridview C#.My Query is during run time in header i''m getting F1,F2,F3 like this...How to get the column name instead of getting F1,F2,F3...
Regards
Balamurugan
推荐答案
您可以看到此链接 ^ ]
只需将标题放在Excel文件中的每一列
查看更多链接
http://sivanandareddyg.blogspot.in/2012/05/read-and-import-excel-sheet-into-aspnet.html [
hi you can see this link http://www.ezzylearning.com/tutorial.aspx?tid=1553779[^]
just put the headings in your Excel file for each column
See one more link
http://sivanandareddyg.blogspot.in/2012/05/read-and-import-excel-sheet-into-aspnet.html[^]
在属性(名称)= txtPath
OpenFileDialog属性(名称)= ofd
一键式属性(名称)= btnBrows
两个RadioButton第一个属性(名称)= rdbY第二个属性(名称)= rdbN
一个DtaGridView属性(名称)= dataGridView1
现在查看代码-.cs
Take one TextBox In the property (Name)=txtPath
OpenFileDialog Property (Name)=ofd
one Button Property (Name)=btnBrows
two RadioButton 1st Property (Name)= rdbY 2nd Property (Name)=rdbN
one DtaGridView Property (Name)=dataGridView1
Now see the code --.cs
private void btnBrows_Click(object sender, EventArgs e)
{
DialogResult result = ofd.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
string file = ofd.SafeFileName;
string Extension = Path.GetExtension(ofd.FileName);
Import_To_Grid(ofd.FileName, Extension, isRDB);
}
}
private void Import_To_Grid(string FilePath, string Extension, string isHDR)
{
string conStr = "";
switch (Extension)
{
case ".xls": //Excel 97-03
conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //Excel 07
conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
conStr = String.Format(conStr, FilePath, isHDR);
OleDbConnection connExcel = new OleDbConnection(conStr);
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter oda = new OleDbDataAdapter();
DataTable dt = new DataTable();
cmdExcel.Connection = connExcel;
//Get the name of First Sheet
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();
//Read Data from First Sheet
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
oda.SelectCommand = cmdExcel;
oda.Fill(dt);
connExcel.Close();
//Bind Data to GridView
dataGridView1.DataSource = dt;
}
string isRDB = string.Empty;
private void rdbY_CheckedChanged(object sender, EventArgs e)
{
isRDB = "Yes";
}
private void rdbN_CheckedChanged(object sender, EventArgs e)
{
isRDB = "No";
}
App.Config设置
App.Config settings
<connectionstrings>
<add name="Excel03ConString" connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties='Excel 8.0;HDR={1}'" />
<add name="Excel07ConString" connectionstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties='Excel 8.0;HDR={1}'" />
</connectionstrings>
右键单击Datagrid视图,然后在该更改中选择属性".Column Header Visible = False.
Right Click Datagrid View and choose Properties in that change Column Header Visible= False.
这篇关于在C#.net的Datagridview中更改标题名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文