我想在gridview中隐藏列。我创建一个数据表并从数据库中添加数据然后用gridview绑定它。现在我想隐藏0索引列,但它没有隐藏。 请帮帮我。
这是我的代码。
`int companyID = Convert.ToInt16(Session [“companyID”]);
DataTable taskTable = new DataTable("TaskList");
SqlConnection sql_connection = new SqlConnection("data source=.\\SQLEXPRESS;Integrated Security=SSPI; initial catalog=db_ease");
SqlCommand sqlCommand = new SqlCommand("select tbl_employee.empID,tbl_employee.empEmploymentID,tbl_employee.empFirstname,tbl_employee.empMiddlename,tbl_employee.empLastname,tbl_employee.empGender,tbl_employee.empMobileNo,tbl_employee.empMac,tbl_association.associationStatus from tbl_employee inner join tbl_association on tbl_employee.empID=tbl_association.empID where tbl_association.companyID="+companyID, sql_connection);
SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand);
adapter.Fill(taskTable);
Session["TaskTable"] = taskTable;
taskTable.Columns[0].ColumnName = "Employee ID";
taskTable.Columns[1].ColumnName = "Employment ID";
taskTable.Columns[2].ColumnName = "First Name";
taskTable.Columns[3].ColumnName = "Middle Name";
taskTable.Columns[4].ColumnName = "Last Name";
taskTable.Columns[5].ColumnName = "Gender";
taskTable.Columns[6].ColumnName = "Mobile No";
taskTable.Columns[7].ColumnName = "MAC Address";
taskTable.Columns[8].ColumnName = "Status";
BindData();
displayGridView.Columns[0].Visible = false;
sql_connection.Close();`
这是我的gridview
<asp:GridView ID="displayGridView" runat="server"
CssClass="table table-striped table-bordered dataTable"
OnRowEditing="displayGridView_RowEditing"
OnRowUpdating="displayGridView_RowUpdating"
OnRowDeleting="displayGridView_RowDeleting"
OnRowCancelingEdit="displayGridView_RowCancelingEdit"
AllowPaging="true" OnPageIndexChanging="displayGridView_PageIndexChanging"
OnSorting="displayGridView_RowSorting"
AutoGenerateDeleteButton="true"
AutoGenerateEditButton="true"
PagerSettings-FirstPageText="First"
PagerSettings-LastPageText="Last"
PagerSettings-Mode="NumericFirstLast"
PagerSettings-PageButtonCount="4" PageSize="4"
EmptyDataText="No data found"
AllowSorting="True"
OnDataBound="displayGridView_OnDataBound"
>
</asp:GridView>
答案 0 :(得分:0)
你应该隐藏displayGridView_RowDataBound事件上的列。
protected void displayGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
}
答案 1 :(得分:0)
当您只需要gridview中的某些列时,您应该只在aspx文件中添加这些列,如下所示:
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField HeaderText="Header Name" DataField="NameOfFieldInDatabase" />
<asp:BoundField HeaderText="Header Name2" DataField="NameOfFieldInDatabase2" />
<asp:BoundField HeaderText="Header Name3" DataField="NameOfFieldInDatabase3" />
</Columns>
</asp:GridView>
你还应该添加AutoGenerateColumns =&#34; false&#34;像上面的代码一样gridview,否则你会有自定义列加上所有其他自动生成的列