The easiest way is you can bind the field (row.Cells[4]) to CommandArgument property of the button which has the CommandName="PointBreakDown"
like this: CommandArgument='<%# Eval("teamID") %>'
Then in the RowCommand you can get this value: string teamID = e.CommandArgument;
Clicking something in the Row does not Select it. That's why SelectedRow is not set.
Typically, CommandName is used in conjunction with CommandArgument. Set it to <%# ((GridViewRow)Container).RowIndex %> and you can use e.CommandArgument in your handler. Convert it to int, and use it as index in ResultsGV.Rows.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
luke_bryant
Member
396 Points
360 Posts
Gridview error
Jul 20, 2012 02:54 PM|LINK
Hi Programmers,
I am trying to get selected colunm values in a gridview depending on what row the user selects below is my code:
protected void GridViewPointsHandler(Object sender, GridViewCommandEventArgs e) { if (e.CommandName == "PointBreakDown") { //Business Layer objects WinnerCheckerBOL.GameIDInformationBOL cgt = new GameIDInformationBOL(Convert.ToString(Session["GameID"])); WinnerCheckerBOL.ValidateDateBOL vdb = new ValidateDateBOL(); GridViewRow row = ResultsGV.SelectedRow; //Get selected values required string teamID = row.Cells[4].Text; } }The problem I have is the string teamID = row.Cells[4].Text produces the following error: Object reference not set to an instance of an object.
Thanks in advance
mebinici
Participant
815 Points
256 Posts
Re: Gridview error
Jul 20, 2012 06:35 PM|LINK
Did you debug and run through the code to see what is not being initialized correctly?
Add a try{}catch block in the if e.commandName statement and see exactly where the error is blowing up on.
I could be your BLL object or gridview row object.
Love collecting video games, movies and board games!
Enjoying my '11 WRX, so sexy...
jbo796
Member
23 Points
25 Posts
Re: Gridview error
Jul 20, 2012 06:54 PM|LINK
Just use an SQL datasource WHERE statement
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Gridview error
Jul 24, 2012 07:48 AM|LINK
Hi,
The easiest way is you can bind the field (row.Cells[4]) to CommandArgument property of the button which has the CommandName="PointBreakDown" like this: CommandArgument='<%# Eval("teamID") %>'
Then in the RowCommand you can get this value: string teamID = e.CommandArgument;
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
superguppie
All-Star
48225 Points
8679 Posts
Re: Gridview error
Jul 26, 2012 01:55 PM|LINK
Clicking something in the Row does not Select it. That's why SelectedRow is not set.
Typically, CommandName is used in conjunction with CommandArgument. Set it to <%# ((GridViewRow)Container).RowIndex %> and you can use e.CommandArgument in your handler. Convert it to int, and use it as index in ResultsGV.Rows.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.