I want to bind the checkbox for Headertext in gridview dynamically in c#.
Here I bind the Gridview From excel .
public void Import(string FilePath, string extension, string IsHDR)
{
string constr = "";
switch (extension)
{
case ".xls":
constr = ConfigurationManager.ConnectionStrings["Excel03ConString"]
.ConnectionString;
break;
case ".xlsx":
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;
connexcel.Open();
DataTable dtexcelsh;
dtexcelsh = connexcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetname = dtexcelsh.Rows[0]["TABLE_NAME"].ToString();
connexcel.Close();
connexcel.Open();
cmdexcel.CommandText = "select * from [" + sheetname + "]";
oda.SelectCommand = cmdexcel;
oda.Fill(dt);
connexcel.Close();
grdBindExcel.Caption = Path.GetFileName(FilePath);
grdBindExcel.DataSource = dt;
grdBindExcel.DataBind();
int count = dt.Columns.Count;
lblaaa.Text =count.ToString();
}
protected void grdBindExcel_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
string FileName = grdBindExcel.Caption;
string Extension = Path.GetExtension(FileName);
string FilePath = Server.MapPath(FolderPath + FileName);
Import(FilePath, Extension , rbHDR.SelectedItem.Text);
grdBindExcel.PageIndex = e.NewPageIndex;
grdBindExcel.DataBind();
}
<asp:GridView ID="grdBindExcel" runat="server"
onpageindexchanging="grdBindExcel_PageIndexChanging" onrowcreated="grdBindExcel_RowCreated"
>
</asp:GridView>
I will Directly Bind the gridview From Excel.