PDA

View Full Version : edit from datagrid



sanaz_amiri
چهارشنبه 30 اردیبهشت 1383, 09:52 صبح
سلام..

یه دیتا گرید دارم که میخوام توش edit انجام بدم.وقتی تو ردیف مورد نظر ستونی که نام edit رو داره میزنیم باید تمام ستونهای اون ردیف به شکل textbox در بیاد
تا بشه edit کردشون
لینکهای بالا رو هم دیدم اما نفهمیدم

میشه راهنماییم کنین (فقط تو textbox اش ایراد دارم)

MFCGalaxy
چهارشنبه 30 اردیبهشت 1383, 10:01 صبح
در تابعی که برای Edit مینویسید احتمالا فراموش کرده اید که دستور زیر را اضافه کنید :


DataGrid.EditItemIndex = e.Item.ItemIndex

sanaz_amiri
چهارشنبه 30 اردیبهشت 1383, 10:48 صبح
مشکل من ساخت اون text box هاست
و update کردنشون
یه چیزی مشابه این
http://authors.aspalliance.com/aldotnet/examples/dgautoscrollexample.aspx#DataGrid1:_ctl2:_ctl0

MFCGalaxy
چهارشنبه 30 اردیبهشت 1383, 14:32 عصر
شما اون کدی که قبلا گفتم را اگر بنویسین ... خودش بطور اتوماتیک اون textbox ها را نشون میده ....

باید سه تا تابع داشته باشین که سه تا کد برای UPDATE , EDIT , CANCEL را توش نوشته باشی .....

راه ساده تر : روی DataGrid کلیک راست کنید و Property Builder را انتخاب کنید .. حالا در قسمت Columns ستونی ار نوی دکمه اضافه کنید .... نوع دکمه را EDIT, UPDATE , CANCEL انتخاب کنید ..............

sanaz_amiri
چهارشنبه 30 اردیبهشت 1383, 15:27 عصر
ممنون

حق با شما بود

متشکرم

اما الان یه error دیگه تو update میده


Specified argument was out of the range of valid values. Parameter name: index


به نظر شما مشکل از کجاست

MFCGalaxy
چهارشنبه 30 اردیبهشت 1383, 19:20 عصر
این را با دقت و حوصله بخون .... مشکلت حل میشه ....


You can edit items in a DataGrid by adding an EditCommandColumn to the DataGrid and associating subroutines with the DataGrid control's EditCommand, UpdateCommand, and CancelCommand events.




<%@ Import Namespace="System.Data.SqlClient" %>

<Script Runat="Server">

Dim conNorthwind As SqlConnection
Dim cmdSql As SqlCommand
Dim strSql As String

Sub Page_Load
conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Northw ind" )
If Not IsPostBack Then
BindDataGrid
End If
End Sub

Sub BindDataGrid
cmdSql = New SqlCommand( "Select * From Products", conNorthwind )
conNorthwind.Open()
dgrdProducts.DataSource = cmdSql.ExecuteReader()
dgrdProducts.DataBind()
conNorthwind.Close()
End Sub

Sub dgrdProducts_EditCommand( s As Object, e As DataGridCommandEventArgs )
dgrdProducts.EditItemIndex = e.Item.ItemIndex
BindDataGrid
End Sub

Sub dgrdProducts_UpdateCommand( s As Object, e As DataGridCommandEventArgs )
Dim intProductID As Integer
Dim txtProductName As TextBox
Dim txtUnitPrice As TextBox
Dim strProductName As String
Dim decUnitPrice As Decimal

intProductID = dgrdProducts.DataKeys( e.Item.ItemIndex )
txtProductName = e.Item.Cells( 1 ).Controls( 0 )
txtUnitPrice = e.Item.Cells( 2 ).Controls( 0 )
strProductName = txtProductName.Text
decUnitPrice = txtUnitPrice.Text
strSql = "Update Products Set ProductName=@ProductName, " _
& "UnitPrice=@UnitPrice Where ProductID=@ProductID"
cmdSql = New SqlCommand( strSql, conNorthwind )
cmdSql.Parameters.Add( "@ProductName", strProductName )
cmdSql.Parameters.Add( "@UnitPrice", decUnitPrice )
cmdSql.Parameters.Add( "@ProductID", intProductID )
conNorthwind.Open()
cmdSql.ExecuteNonQuery()
conNorthwind.Close()
dgrdProducts.EditItemIndex = -1
BindDataGrid
End Sub

Sub dgrdProducts_CancelCommand( s As Object, e As DataGridCommandEventArgs )
dgrdProducts.EditItemIndex = -1
BindDataGrid
End Sub

</Script>

<html>
<head><title>DataGridEditProducts.aspx</title></head>
<body>
<form Runat="Server">

<asp:DataGrid
ID="dgrdProducts"
OnEditCommand="dgrdProducts_EditCommand"
OnUpdateCommand="dgrdProducts_UpdateCommand"
OnCancelCommand="dgrdProducts_CancelCommand"
DataKeyField="ProductID"
AutoGenerateColumns="False"
CellPadding="10"
HeaderStyle-BackColor="Salmon"
Runat="Server">
<Columns>
<asp:BoundColumn
HeaderText="Product ID"
DataField="ProductID"
ReadOnly="True" />
<asp:BoundColumn
HeaderText="Product Name"
DataField="ProductName" />
<asp:BoundColumn
HeaderText="Price"
DataField="UnitPrice"
DataFormatString="{0:c}" />
<asp:EditCommandColumn
EditText="Edit!"
UpdateText="Update!"
CancelText="Cancel!" />
</Columns>
</asp:DataGrid>

</form>
</body>
</html>




When you select an item for editing, the dgrdProducts_EditCommand subroutine executes. This subroutine sets the value of the EditItemIndex property of the DataGrid. Any bound columns selected for editing automatically appear with TextBox controls.

When a product is selected for editing in Listing 11.22, the BoundColumns for the ProductName and UnitPrice appear with TextBox controls. The ProductID column is excluded from editing because its ReadOnly property is set to the value True.

When the Update button is clicked, the dgrdProducts_UpdateCommand subroutine executes. This subroutine retrieves the value of the ProductID associated with the row selected for editing by retrieving the value from the DataKeys collection.

The values of the TextBox controls named ProductName and UnitPrice are retrieved with the following lines:



txtProductName = e.Item.Cells( 1 ).Controls( 0 )
txtUnitPrice = e.Item.Cells( 2 ).Controls( 0 )
strProductName = txtProductName.Text
decUnitPrice = txtUnitPrice.Text

Each row of a DataGrid consists of cells, and each cell contains controls. The TextBox control named txtProductName is retrieved by grabbing the first control located in the second cell of the DataGrid. (The first cell contains the ProductID.) The TextBox control named txtUnitPrice is retrieved by grabbing the first control in the third cell.

After the TextBox controls named txtProductName and txtUnitPrice have been retrieved, the current values of these TextBox controls can be assigned to the strProductName and decUnitPrice variables.

After a product is updated in the database, the DataGrid control's EditItemIndex property is assigned the value -1. This value unselects any item for editing in the DataGrid


:خیلی متعجب: 8)