I'm trying to hide a column of a GridView. I've a template field in my GridView. Code that I'm applying to hide the desired column works well without having template field in the GridView. But because I need to have template field in my GridView, when I add it, code is giving error.
GridView (.aspx) code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="5" AutoGenerateColumns="True" CellSpacing="4" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4" DataKeyNames="File_Name" GridLines="None" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" ForeColor="#333333" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnPageIndexChanging="GridView1_PageIndexChanging" AllowSorting="True" OnSorting="GridView1_Sorting" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnRowCreated="GridView1_RowCreated">
<PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="10" Position="Bottom" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="File">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnFileName" runat="server" CommandArgument='<%# Eval("File_Name") %>' CommandName="Download" Text='<%# Eval("File_Name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
.cs code for PageLoad() event is:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS; Initial Catalog=PIMS; Integrated Security=true;");
{
using (SqlCommand cmd = new SqlCommand())
{
String sql = "select * from dbo.Documents";
cmd.Connection = con;
cmd.CommandText = sql;
con.Open();
DataSet ds = new DataSet();
using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
{
adp.Fill(ds);
}
DataTable mytable = ds.Tables[0];
GridView1.DataSource = mytable;
GridView1.DataBind();
MultiView1.SetActiveView(vHome);
btnBacktoHome.Visible = false;
lblStatus.Visible = false;
}
}
}
}
RowCreated() event code is:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
DataTable myGridDataTable = null;
TableCell tblcell1;
myGridDataTable = (DataTable)((GridView)sender).DataSource;
if (myGridDataTable != null)
{
tblcell1 = e.Row.Cells[myGridDataTable.Columns["DocumentsID"].Ordinal];
tblcell1.Visible = false;
}
}
Above code runs and brings desired results if I remove <TemplateField> from <GridView1> .aspx code. Can anyone tell me why number of enumerated cells in `e.Row.Cells` in GridView1_RowCreated() event is 1 when myGridDataTable is filled with datatable having 25 columns. ? Thanks in advance.