HOW TO: Search, insert or remove the DataGrid line (from MSDN) after you are sorted by using Visual C # .NET, insert or remove D

xiaoxiao2021-03-06  71

This task content

Summary

Code example for retrieve the current row of DataGrid after you sort, insert or delete rows, confirm that it can use

Summary This article is described in your order, insert or delete it.

How to retrieve the current election after the DataGrid

DataGrid line. When you sort, insert or delete

After the DataGrid line, the currency manager still retains data in actual format. edited

DataGrid's default view without modifying the currency manager.

DataGrid's default view combines any sort, insert, or delete the row. You can use the default view to retrieve the currently selected

DataGrid line.

Back to top

Code example for retrieving the current row of DataGrid after you sort, insert or delete rows, to retrieve, insert or delete lines after you

DataGrid's current line, please follow these steps:

In Microsoft Visual Studio .NET, create a new Windows application project using Microsoft Visual Basic .NET or Microsoft Visual C # .NET. By default, "FORM1" will be created. On the Toolbar's Data tab, double-click "SqlDataAdapter" and click Next. Click New Connection. Type your Microsoft SQL Server username and password to connect to SQL Server. From the "Select Server Database" list, click "Northwind" and click OK. After creating this connection, click Next, then click Next. On the "Generate SQL Statement" page, type SQL statement Select * from customerS, and then click Finish. Right-click "SqlDataAdapter1" and click Generate Data Set. Click OK. From the toolbox, drag the DataGrid control to "Form1". Add two "Buttons" controls from the toolbox to "Form1" and two "text box" controls. In the Solution Explorer, right-click "DataSet1" and click View Code. Add the following code to bind "DataGrid" with "DataSet": Private Sub Form1_Load (Byvale AS System.Object, ByVal E AS System.EventArgs) Handles MyBase.Load

Button1.text = "Get Current Row"

Button2.text = "sort on city"

'Fill the Dataset with the data from the database

Me.sqldataAdapter1.fill (me.dataset11)

'Bind the datagrid with the datasource

DataGrid1.datasource = dataset11.tables (0) .defaultVIEW

End Sub adds the following code to the "Button1_Click" event handler to retrieve the current line of data: Private Sub Button1_Click (Byval E AS System.EventArgs) Handles Button1.click

'Get the currency manager by using the bindingcontext of the datagrid

DIM CM As CurrencyManager = CType (Me.BindingContext (DataGrid1.DataSource, DataGrid1.datamember), currencymanager 'Retrieve The Default DataView of The DataGrid

DIM DV AS DATAVIEW = CTYPE (cm.list, data)

'Use currency manager and dataview to retrieve the current row

DIM DR AS DATAROW

DR = DV.Item (cm.position) .row

'Display THE CURRENT ROW DATA

TextBox1.text = DR (0) .tostring

TextBox2.text = DR (1) .tostring

End Sub adds the following code to the "Button2_Click" event handler to rule data in "DataGrid": Private Sub Button2_Click (Byval E AS System.EventArgs) Handles Button2.click

'Sort the data

DataSet11.tables (0) .defaultview.sort = "city"

'Refresh the grid to display the sorted data

DataGrid1.refresh ()

End Sub On the Generate menu, click Generate Solution.

Back to top

Confirm that it can be used to verify that it is working properly, follow the steps below:

On the Debug menu, click Start to run the application. Click in "DataGrid". Click "Get the current line". The current row data is displayed in "Text Box". Click "Sort by City". The rows in "DataGrid" will be sorted according to the city. Click "Get the current line" again. The current row data is displayed in "Text Box".

Back to top

Refer to additional information, click the article number below to see the article in the Microsoft Knowledge Base:

308070 HOW TO: Implement a Searchable DataGrid by Using ADO.NET and Windows Forms

313154 How to: Create a summary row for a datagrid in asp.net by using visual basic .net

Back to top

The information in this article applies to:

Microsoft Visual Basic .NET (2003) Microsoft Visual Basic .NET (2002)

Recent Updated: 2003-12-23 (2.0) Keywords: KBWindowsForms KBSystemData KBSQLClient KbdataBinding Kbdatabase KbdataAdapter KbhowTomaster KB817247 KBAUDDEVELOPER

转载请注明原文地址:https://www.9cbs.com/read-109075.html

New Post(0)