View Full Version : سوال: نمایس یک سطر از grid view در lable
barbii
شنبه 21 مرداد 1391, 16:01 عصر
سلام
میخوام مقدار یک سطر از grid view داخل یه lable نمایش داده بشه با find control
alonemm
شنبه 21 مرداد 1391, 16:22 عصر
باسلام:
در قالب یک پروژه:
فرض کنید که این وب فرم ما هست که به کدش به شکل زیر هست که دارای یک کنترل گرید و یک شی اتصال به پایگاه داده SQL:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView Select Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView Select Example</h3>
<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"/>
</form>
</body>
</html>
خب با انتخاب هر ردیف از شی گرید به مقدار اون در لیبل دسترسی دارید همانند زیر:
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 + ".";
}
موفق باشید.
barbii
شنبه 21 مرداد 1391, 17:17 عصر
باسلام:
در قالب یک پروژه:
فرض کنید که این وب فرم ما هست که به کدش به شکل زیر هست که دارای یک کنترل گرید و یک شی اتصال به پایگاه داده SQL:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView Select Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView Select Example</h3>
<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"/>
</form>
</body>
</html>
خب با انتخاب هر ردیف از شی گرید به مقدار اون در لیبل دسترسی دارید همانند زیر:
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 + ".";
}
موفق باشید.
ممنون از راهنمایی تون دوست عزیز
در حقیقت من میخوام لیست نمرات دانش آموزان که داخل یک grid view هستو با یه تکس باکس دریافت کنم و همراه با شماره دانش آموزی در بانک ذخیره کنم می خوام تک تک همه رکوردا رو بدست بیاره و ذخیره کنه
alonemm
شنبه 21 مرداد 1391, 18:14 عصر
ممنون از راهنمایی تون دوست عزیز
در حقیقت من میخوام لیست نمرات دانش آموزان که داخل یک grid view هستو با یه تکس باکس دریافت کنم و همراه با شماره دانش آموزی در بانک ذخیره کنم می خوام تک تک همه رکوردا رو بدست بیاره و ذخیره کنه
درود:
به عنوان مثال به شکل زیر عمل کنید:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
//for templated control
TextBox tb = row.FindControl("yourtextboxid") as TextBox;
//for bound field
string str = row.Cells[yourcellindex];
}
}
و یا:
for (int i = 0; i < GridView1.Rows.Count ;i++)
{
String str;
//To Read a TextBox in First Column
TextBox txt = (TextBox)GridView1.Rows[i].Cells[0] .FindControl("txtFirstName");
str = txt.Text;
//To Read a Label in First Column
Label lbl = (Label)GridView1.Rows[i].Cells[0].FindControl("LabelFirstName");
str = lbl.Text;
//To Read a Cell in First Column
str = GridView1.Rows[i].Cells[0].Text ;
}
vBulletin® v4.2.5, Copyright ©2000-1403, Jelsoft Enterprises Ltd.