帮助从GridView获取所选行的值 [英] Help on getting the value of selected row from GridView
问题描述
大家好,
请接受我糟糕的节目,因为我是.Net世界的新手。
以下是GridView的aspx代码,它显示用户在数据库中完成的搜索结果。
Hi all,
Kindly bear with my poor programming as I am new to .Net world.
Following is a aspx code for GridView which displays results of search done in the database by user.
<div>
<asp:GridView ID="gvwKeyList" runat="server" AutoGenerateColumns="False" EmptyDataText="No data available."
onrowcommand="GridView_RowCommand"
CssClass="table table-striped table-condensed" BorderStyle="None" AlternatingRowStyle-BackColor="#C0DC3B">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="b1" runat="server" Text="Attach?" CommandName="Select" class="btn btn-primary" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="key_id" HeaderText="Key Id" />
<asp:BoundField DataField="key_no" HeaderText="Key No" />
<asp:BoundField DataField="hook" HeaderText="Hook" />
<asp:BoundField DataField="access_description" HeaderText="Accessible?" />
<asp:BoundField DataField="key_type_description" HeaderText="Key Type" />
<asp:BoundField DataField="key_location" HeaderText="Key Location" />
</Columns>
<SelectedRowStyle BackColor="LightCyan" ForeColor="DarkBlue" Font-Bold="true" />
</asp:GridView>
这个工作正常,我可以轻松地显示它。这是一个代码。
This is working fine and I can easily display it. Here is a code for it.
protected void search_keys(object sender, EventArgs e)
{
string key = txt_key_no.Text;
string hook = txt_hook.Text;
List<Key> lstKey = new List<Key>();
key_dal objDAL = new key_dal();
lstKey = objDAL.GetKeyListByKeyHook(key, hook);
gvwKeyList.DataSource = lstKey;
gvwKeyList.GridLines = GridLines.None;
gvwKeyList.DataBind();
}
当我想在用户点击附加?时获取Key_id的值时,问题就出现了。按钮。无论我尝试过什么方法,我总是会遇到错误。我无法得到RowIndex。以下是GridView_RowCommand方法的代码
My problem starts when I want to get the value of "Key_id" when user clicks on "Attach?" button. No matter what method I have tried I am always getting an error. I cannot get the RowIndex. Here is the code for "GridView_RowCommand" method
protected void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Get the last name of the selected author from the appropriate
// cell in the GridView control.
GridViewRow selectedRow = gvwKeyList.Rows[index];
TableCell contactName = selectedRow.Cells[1];
string contact = contactName.Text;
// Display the selected text.
Message.Text = "You selected " + contact + ".";
}
}
请有人请指导我的错误。我不介意我是否必须放弃这种方式并采取新的更好的方法。
非常感谢提前
对不起。有人正确地指出我的意思是我总是得到一个错误。这是我最后4个小时的挫折,使我无法做出任何事情。这些是我试过的各种各样的东西,但总是得到一个null异常错误。
Can somebody please please guide me my mistake. I don't mind if I have to abandon this way and take up something new and better if available.
Many thanks in advance
Sorry. Somebody pointed out correctly that what do I mean by "I am always getting an error". It was my last 4 hours of frustration which prevented me from assuming things. These are the
various things I tried but always got a null Exception error.
//int rowindex = gvwKeyList.SelectedRow.RowIndex;
//string stVal = gvwKeyList.Rows[rowindex].Cells[1].ToString();
OR
//int selectedrowindex = gvwKeyList.SelectedRow.RowIndex;
//GridViewRow selectedRow = gvwKeyList.Rows[selectedrowindex];
//string stVal = Convert.ToString(selectedRow.Cells[1].ToString());
OR
//GridViewRow row = gvwKeyList.SelectedRow;
//GridViewRow clik = ((Button)sender).NamingContainer as GridViewRow;
//string stVal = clik.FindControl("key_id").ToString();
OR
// Determine the index of the selected row.
//int index = gvwKeyList.SelectedIndex;
//int selected_key_id = Convert.ToInt32(gvwKeyList.DataKeys[index].Value);
//int selected_key_id = Convert.ToInt16(gvwKeyList.SelectedRow.Cells[1].Text);
Message.Text = "The primary key value of the selected row is " + stVal + ".";
我终于放弃了它们并选择了protected void GridView_RowCommand方法,它给了我一个输入字符串格式不正确 错误。我想要的只是一行中我点击按钮的值
I finally abandoned them and went for the "protected void GridView_RowCommand" method which gave me a "Input string was not in a correct format" error. All I want is a value from the row where I click the button
推荐答案
所以看来你的主要问题是无法获得GridView的OnRow命令事件中的行索引
..对吗?
如果是这样,那么以下链接肯定可以解决您的问题。
看看吧。
获取Asp.net Rowcommand事件的行索引
AND
gridview rowCommand中的行索引
i希望你能轻松获得key_id
获取行索引
后,虽然有任何进一步的问题,请更新您的问题,并发表评论。
:)
So it seems that your main problem is "Failed to get the Row index in OnRow Command Event of the Gridview
" .. Right?
if it is so, than following links can surely solve your problem.
Do have a look at .
Get Row Index on Asp.net Rowcommand event
AND
row index in gridview rowCommand
i hope you will be easily able to get thekey_id
after getting therow index
, Though incase of any further issue, Do update your question, and Leave a comment Please.
:)
这篇关于帮助从GridView获取所选行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!