Simple Selecting a row from gridview in asp.net web application
Solution 1
if i get it right, this should do what you want:
.aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" onselectedindexchanged="GridView1_SelectedIndexChanged">
code behind:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = Convert.ToInt16(GridView1.SelectedDataKey.Value);
}
this should do what you want..index will give you the selected row ID, provided by the DataKeyNames
attribute in your .aspx page. This however does require the "Enable Selection" to be checked. (Go to your .aspx page, designer, click your gridview, you should see the "Enable selection" attribute).
Solution 2
If you are adding a DataSource from code behind file then you have to set property called "AutoGenerateSelectButton " to True . This will enable you to select a row.
TBogdan
Updated on February 15, 2020Comments
-
TBogdan about 4 years
I know this question had been asked a hundred times, but I have difficulties implementing different solutions.I need to retrieve a selected row from a grid view in asp.net web application C#.I've done the databinding.I don't want to use edit/update buttons or checkboxe/radio button, just to select it by clicking on the row.Please help, I'm a little stuck, and I would like not to implement javascript based solutions.Thanks.
if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("OnMouseOver", "this.style.cursor='pointer';this.style.textDecoration='underline';"); e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; e.Row.ToolTip = "Click on select row"; e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(this.SingleSelectGrid, "Select$" + e.Row.RowIndex); LinkButton selectbutton = new LinkButton() { CommandName = "Select", Text = e.Row.Cells[0].Text }; e.Row.Cells[0].Controls.Add(selectbutton); e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(selectbutton, ""); }
-
TBogdan almost 12 yearsI'm not finding the Enable Selection attribute, you can tell me the .aspx code to enable it...I'm using visual studio 2010
-
TBogdan almost 12 yearsthe bad news is that i'm binding my data manually from an arraylist...in my code...I think that's why I don't see the 3 enables..so I see it unbound untill my application starts...
-
TBogdan almost 12 yearsah....and I mentioned that I wanted not to use buttons...such as the linkbutton that appears when enable selection is activated
-
TBogdan almost 12 yearsfirst I had some error related to Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
-
TBogdan almost 12 yearsand, another issue, is that, I don't know exaclty how to use that to retrieve the selected row...can u help me?:)) giving me an example ?
-
Thousand almost 12 yearswhy are you adding that linkbutton to your gridview programatically? you could just do as i said: insert an an itemtemplate into your .aspx code with a commandName (like you already have)
-
Thousand almost 12 yearsi dont understand why you dont want a button in that gridview. e.Row.Cells[0].Controls.Add(selectbutton); this should insert a button into your gridview, regardless..
-
TBogdan almost 12 yearsok, thanks, really, I now know that I have no choice, but , can I at least go for RadioButton?
-
Thousand almost 12 yearsyou want to put a radiobutton on every row, to see what row is being clicked? That's not really a good way of doing it, i really dont understand why you dont just use a linkbutton or a button, it would be so much simpler and easier lol
-
TBogdan almost 12 yearsbecause...this is my license project and I would want it to look nice...I had a bad ideea doing it in asp.net because I'm newbie on asp...but too late now...so that's why I would like it to be special:)
-
Thousand almost 12 yearsthere is absolutely no reason to NOT do something in asp.net because you want it to look nice. ASP.NET enables you to make stuff look really nice and special, through CSS, theme skins, ... i would advice you to look into those things. Also take a look at JQuery UI, if you really want to make it look special.
-
TBogdan almost 12 yearsok, thanks man for your patience, really:), in this case I will be back soon with questions regarding JQuery UI,have a good day, or night, here is 2:25 AM
-
Thousand almost 12 yearsno problem, thanks for the accept/upvote :). just follow some ASP.net tutorials, try to understand it a little more before actually diving in :) good luck