Using <asp:GridView></asp:GridView>
I am trying to display columns of database table. I want to fetch primary key of table but obviously don't want to display it. How can I achieve this?
This is my GridView
使用
<asp:GridView ID="MyGrid" runat="server" BorderColor="#0066FF" AllowPaging="false" PageSize="5" AllowSorting="true" AutoGenerateColumns="false"
AutoGenerateEditButton="true" OnRowEditing="MyGrid_RowEditing" AutoGenerateDeleteButton="true" OnRowDeleting="MyGrid_RowDeleting"
OnRowDataBound="MyGrid_RowDataBound" EmptyDataText="No Value" BorderWidth="0px" BorderStyle="Solid">
<Columns>
<asp:BoundField DataField="PrimaryKey" HeaderText="UserId"/>
<asp:BoundField DataField="Column1" HeaderText="Column1" />
<asp:BoundField DataField="Column2" HeaderText="Column2" />
<asp:BoundField DataField="Column3" HeaderText="Column3" />
</Columns>
</asp:GridView>
I also tried visible=false
to hide primary key, it hides primary key from displaying but also don't fetch its value and I want this value.
Hope my question is clear.
我也尝试使用visible = false来隐藏主键,它隐藏了主键,但也没有获取它的值,我想要这个值。希望我的问题很清楚。
5 个解决方案
#1
6
You need to set Visible = false within the OnRowDataBound event, this will mean the data is still accessible to you, but won't display on the page.
您需要在OnRowDataBound事件中设置Visible = false,这意味着您仍然可以访问数据,但不会在页面上显示。
<asp:GridView ID="MyGrid" runat="server" BorderColor="#0066FF"
AllowPaging="false" PageSize="5" AllowSorting="true"
AutoGenerateColumns="false" AutoGenerateEditButton="true"
OnRowEditing="MyGrid_RowEditing" AutoGenerateDeleteButton="true"
OnRowDeleting="MyGrid_RowDeleting"
OnRowDataBound="MyGrid_RowDataBound" EmptyDataText="No Value"
BorderWidth="0px" BorderStyle="Solid">
<Columns>
<asp:BoundField DataField="PrimaryKey" HeaderText="UserId"/>
<asp:BoundField DataField="Column1" HeaderText="Column1" />
<asp:BoundField DataField="Column2" HeaderText="Column2" />
<asp:BoundField DataField="Column3" HeaderText="Column3" />
</Columns>
</asp:GridView>
In Codebehind:
protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
}
#2
2
<asp:GridView ... DataKeyNames="PrimaryKey" />
#3
1
You can use this:
你可以用这个:
<asp:BoundField DataField="PrimaryKey" visible="false" HeaderText="PKId"/>
Also use this
也用这个
#4
0
<asp:BoundField DataField="PrimaryKey" visible="false" HeaderText="UserId"/>
To get Primary Key
获取主键
<asp:GridView ID="MyGrid" runat="server" BorderColor="#0066FF" DataKeyNames="PrimaryKey"
MyGrid.DataKeys[e.NewEditIndex].Value
OR
MyGrid.Rows[e.NewEditIndex].DataBoundItem
#5
0
If you want to hide a column by its name instead of its index in GridView. After creating DataTable or Dataset, you have to find the index of the column by its name then save index in global variable like ViewStae, Session and etc and then call it in RowDataBound, like the example:
如果要在GridView中按名称而不是其索引隐藏列。创建DataTable或Dataset后,您必须按名称查找列的索引,然后将索引保存在全局变量中,如ViewStae,Session等,然后在RowDataBound中调用它,如下例所示:
string headerName = "Id";
DataTable dt = .... ;
for (int i=0;i<dt.Columns.Count;i++)
{
if (dt.Columns[i].ColumnName == headerName)
{
ViewState["CellIndex"] = i;
}
}
... GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Footer)
{
int index = Convert.ToInt32(ViewState["CellIndex"]);
e.Row.Cells[index].Visible = false;
}
}
#1
6
You need to set Visible = false within the OnRowDataBound event, this will mean the data is still accessible to you, but won't display on the page.
您需要在OnRowDataBound事件中设置Visible = false,这意味着您仍然可以访问数据,但不会在页面上显示。
<asp:GridView ID="MyGrid" runat="server" BorderColor="#0066FF"
AllowPaging="false" PageSize="5" AllowSorting="true"
AutoGenerateColumns="false" AutoGenerateEditButton="true"
OnRowEditing="MyGrid_RowEditing" AutoGenerateDeleteButton="true"
OnRowDeleting="MyGrid_RowDeleting"
OnRowDataBound="MyGrid_RowDataBound" EmptyDataText="No Value"
BorderWidth="0px" BorderStyle="Solid">
<Columns>
<asp:BoundField DataField="PrimaryKey" HeaderText="UserId"/>
<asp:BoundField DataField="Column1" HeaderText="Column1" />
<asp:BoundField DataField="Column2" HeaderText="Column2" />
<asp:BoundField DataField="Column3" HeaderText="Column3" />
</Columns>
</asp:GridView>
In Codebehind:
protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
}
#2
2
<asp:GridView ... DataKeyNames="PrimaryKey" />
#3
1
You can use this:
你可以用这个:
<asp:BoundField DataField="PrimaryKey" visible="false" HeaderText="PKId"/>
Also use this
也用这个
#4
0
<asp:BoundField DataField="PrimaryKey" visible="false" HeaderText="UserId"/>
To get Primary Key
获取主键
<asp:GridView ID="MyGrid" runat="server" BorderColor="#0066FF" DataKeyNames="PrimaryKey"
MyGrid.DataKeys[e.NewEditIndex].Value
OR
MyGrid.Rows[e.NewEditIndex].DataBoundItem
#5
0
If you want to hide a column by its name instead of its index in GridView. After creating DataTable or Dataset, you have to find the index of the column by its name then save index in global variable like ViewStae, Session and etc and then call it in RowDataBound, like the example:
如果要在GridView中按名称而不是其索引隐藏列。创建DataTable或Dataset后,您必须按名称查找列的索引,然后将索引保存在全局变量中,如ViewStae,Session等,然后在RowDataBound中调用它,如下例所示:
string headerName = "Id";
DataTable dt = .... ;
for (int i=0;i<dt.Columns.Count;i++)
{
if (dt.Columns[i].ColumnName == headerName)
{
ViewState["CellIndex"] = i;
}
}
... GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Footer)
{
int index = Convert.ToInt32(ViewState["CellIndex"]);
e.Row.Cells[index].Visible = false;
}
}