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();
}
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")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = gvwKeyList.Rows[index];
TableCell contactName = selectedRow.Cells[1];
string contact = contactName.Text;
Message.Text = "You selected " + contact + ".";
}
}
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 + ".";
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