retrieve gridview values by column name

Posted by Prabhakar under ASP.NET on 9/27/2011 | Points: 10 | Views : 7085 | Status : [Member] [MVP] | Replies : 4
Hi Guy's


i want to Gridview values by Columname not Index Wise . .

i am using this

 GridViewRow row = gv1.SelectedRow;
txtfoodshopname.Text = row.Cells[5].Text;


but i want to this . .

 GridViewRow row = gv1.SelectedRow;
txthopname.Text = row.Cells["Shopname"].Text;


Advised it . . .

Best Regard's
Prabhakar



Responses

Posted by: Ndebata on: 9/28/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
You can get the index from the Column Name then use it as you are using.
var _colindex=gvl.Columns.Cast<DataControlField>().ToList().FindIndex(c => c.HeaderText.Equals("Shopname" ,StringComparison.InvariantCultureIgnoreCase));


GridViewRow row = gv1.SelectedRow;
txtfoodshopname.Text = row.Cells[_colindex ].Text;



Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 9/28/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down

Hi Ndebata,

thanks for replying . . i am try this but some error are occurred . . i am use this code.. on Gridview Select Index Changed Event . .
See that . .

Compiler Error Message: CS1928: 'System.Web.UI.WebControls.DataControlFieldCollection' does not contain a definition for 'Cast' and the best extension method overload 'System.Data.EnumerableRowCollectionExtensions.Cast<TResult>(System.Data.EnumerableRowCollection)' has some invalid arguments


Best Regard's
Prabhakar

Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 10/12/2011 [Member] Starter | Points: 25

Up
0
Down
Dim id As String = CType(gv.HeaderRow.Cells(0).FindControl("lbl"), Label).Text

try using lik this

Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 10/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Prabhakar,

Could you please post your code, what you have tried and did not work?

Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response