I know the question I'm going to ask is already asked for by other people, but those answers are no solution for my problem.
我知道其他人已经提出了我要问的问题,但这些答案对我的问题没有解决方法。
I have a gridview containing 2 BoundFields, 2 ButtonFields and a checkbox field (which is a TemplateField).
我有一个gridview包含2个BoundFields,2个ButtonFields和一个复选框字段(这是一个TemplateField)。
I also have a datatable, filled with data from the database.
我还有一个数据表,填充了数据库中的数据。
In the aspx code I create my gridview with the fields set the last BoundField Visible = false
.
在aspx代码中,我创建了gridview,其中字段设置了最后一个BoundField Visible = false。
In my codebehind, I add the colums and bind the datasource to my datatable.
在我的代码隐藏中,我添加了colums并将数据源绑定到我的数据表。
But when I try to read the data from the hidden boundfield, that field is empty. The problem why I can't use the solutions mentioned with similar questions, is because the people assume the data gets filled in one by one, and not by binding a datatable to the datasource of the gridview.
但是当我尝试从隐藏的边界字段读取数据时,该字段为空。我不能使用类似问题提到的解决方案的问题是因为人们假设数据被逐一填充,而不是通过将数据表绑定到gridview的数据源。
So my question is: Is their a way to get the data from the hidden boundfield and also retain the possibility to add the data by binding the datatable to the datasource of the gridview? And if so, is it posible to get the value from that field?
所以我的问题是:他们是一种从隐藏的边界域获取数据的方法,还可以通过将数据表绑定到gridview的数据源来保留添加数据的可能性吗?如果是这样,从该领域获得价值是否可行?
p.s. I'm using asp.net/c# in visual studio 2010
附:我在visual studio 2010中使用asp.net/c#
ASPX:
ASPX:
<asp:GridView ID="gvSelect" runat="server" AutoGenerateColumns="False" BorderStyle="None" onrowcommand="gvTestSelect_RowCommand">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server" ID="cbHeader" OnPreRender="cbHeader_PreRender" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="cbItems" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="field" HeaderText="Veld" SortExpression="field" />
<asp:ButtonField DataTextField="up" HeaderText="Omhoog" SortExpression="up" CommandName="up" Text="↑" />
<asp:ButtonField DataTextField="down" HeaderText="Omlaag" SortExpression="down" CommandName="down" Text="↓" />
<asp:BoundField DataField="hidden" SortExpression="hidden" />
</Columns>
</asp:GridView>
Code Behind (where I fill the gridview):
Code Behind(填写gridview的地方):
//create array list and fill it with all columns
Dictionary<string, string> dict = FillLists.getColumnsByTable(loader, ddlInfoTableI.SelectedItem.Value.ToString());
//loop trough dictionary
foreach (var val in dict)
{
//create new dtSelect datarow
DataRow dr = dtSelect.NewRow();
//set row values for column values
dr["select"] = false;
dr["field"] = val.Value.ToString();
dr["up"] = new ButtonField { CommandName = "up", Text = loader.LoadResourceString(1024), HeaderText = "↑", ButtonType = ButtonType.Button, Visible = true };
dr["down"] = new ButtonField { CommandName = "down", Text = loader.LoadResourceString(1025), HeaderText = "↓", ButtonType = ButtonType.Button, Visible = true };
dr["hidden"] = val.Key.ToString();
//add the datarow
dtSelect.Rows.Add(dr);
//set datatable session to datatable
Session["dtSelect"] = dtSelect;
//set datasource of the gridview to datatable
gvSelect.DataSource = dtSelect;
//bind data to gridview
gvSelect.DataBind();
}
So now I need to get the data from the gridview (escpecialy from the hidden boundfield), because they can edit the gridview except the hidden boundfield, so that is the only way to know which original row it was.
所以现在我需要从gridview获取数据(特别是隐藏的boundfield),因为除了隐藏的boundfield之外,他们可以编辑gridview,因此这是了解它的原始行的唯一方法。
3 个解决方案
#1
13
Add a data key for the column you need to get:
为您需要获取的列添加数据键:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="hidden" ...>
Once you've added the datakey, you can access its value with the row index:
添加datakey后,可以使用行索引访问其值:
var rowIndex = 0;
var hiddenValue = (string)GridView1.DataKeys[rowIndex]["hidden"];
#2
2
Another option is to keep the BoundField (or any other type of column) visible in the markup (remove Visible=False), and use the RowDataBound event to hide the columns:
另一个选项是使标记中的BoundField(或任何其他类型的列)可见(删除Visible = False),并使用RowDataBound事件隐藏列:
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[COLUMN_USERID].Visible = false;
}
#3
1
you can use this code. i tested it.
你可以使用这段代码。我测试了它。
TextBox1.Text = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
TextBox1.Text = GridView1.DataKeys [e.NewSelectedIndex] .Value.ToString();
#1
13
Add a data key for the column you need to get:
为您需要获取的列添加数据键:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="hidden" ...>
Once you've added the datakey, you can access its value with the row index:
添加datakey后,可以使用行索引访问其值:
var rowIndex = 0;
var hiddenValue = (string)GridView1.DataKeys[rowIndex]["hidden"];
#2
2
Another option is to keep the BoundField (or any other type of column) visible in the markup (remove Visible=False), and use the RowDataBound event to hide the columns:
另一个选项是使标记中的BoundField(或任何其他类型的列)可见(删除Visible = False),并使用RowDataBound事件隐藏列:
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[COLUMN_USERID].Visible = false;
}
#3
1
you can use this code. i tested it.
你可以使用这段代码。我测试了它。
TextBox1.Text = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
TextBox1.Text = GridView1.DataKeys [e.NewSelectedIndex] .Value.ToString();