PDA

View Full Version : سوال: دسترسی به ستون های سطر انتخاب شده در Gridview



mona_bh
پنج شنبه 29 تیر 1391, 12:23 عصر
با سلام

لطفا هر کسی جواب این سوال رو میدونه راهنماییم کنه : چطوری می تونم وقتی کاربر یک سطری از Gridview را انتخاب کرد من بدونم مقدار ستون nام آن سطر چند است؟

alonemm
پنج شنبه 29 تیر 1391, 13:22 عصر
باسلام:

برای دسترسی به مقادیر و یا کنترل های، یک ردیف انتخاب شده از شی Gridview به موارد زیر دقت کنید:

1- گزینه autogenerateselectbutton رو از شی گرید برابر True قرار دهید.
2- برای دسترسی به مقادیر ما از متد onselectedindexchanged شی گرید استفاده میکنیم.
3- اندیس Cells مشخص کننده شماره ستون مورد نظر میباشد.

به مثال زیر توجه کنید:


<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="False"
autogenerateselectbutton="True"
allowpaging="True"
selectedindex="1"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged"

runat="server" DataKeyNames="CustomerID">

<Columns>
<asp:BoundField DataField="CustomerID"
HeaderText="CustomerID"
InsertVisible="False" ReadOnly="True"
SortExpression="CustomerID" />
<asp:BoundField DataField="FirstName"
HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="MiddleName"
HeaderText="MiddleName"
SortExpression="MiddleName" />
<asp:BoundField DataField="LastName"
HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="Phone"
HeaderText="Phone"
SortExpression="Phone" />
</Columns>

<selectedrowstyle backcolor="LightCyan"
forecolor="DarkBlue"
font-bold="true"/>

</asp:gridview>

<br/>

<asp:label id="MessageLabel"
forecolor="Red"
runat="server"/>

<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSource"
selectcommand="SELECT CustomerID, FirstName, MiddleName, LastName, Phone FROM SalesLT.Customer"
connectionstring="<%$ ConnectionStrings:AdventureWorksLTConnectionString %>"
runat="server"/>



در فایل CS:

void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{

// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;

// Display the company name from the selected row.
// In this example, the third column (index 2) contains
// the company name.
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";

}



موفق باشید.