datagridview--administrator-to-user point of Operation

Source: Internet
Author: User

Remember the first time to do the computer room charge system, in addition to delete the user this appeared a little problem, because has always been a little flaw in the person. So the query for the user when the query will be more empty line I am very intolerable. Seemingly very small problem, I took a very long time. Now the computer room charge system is reconstructed. Again in this piece of fastidious , spent a full two hours of time.

A lot of small problems have been overcome.

( 1 Why is the first row of data always displayed?

The user is returned to the U layer using the SqlHelper query to a certain level . Is the traditional three-layer query idea. There was a little bit of joy when the query came out. However, when joining the user to join more, using DataGridView is only to display the first row of data.

take a look at my original code:

        <span style= "FONT-SIZE:14PX;" >dim USERBLL as Bll.usermanager = new Bll.usermanager ()        Dim dt as New DataTable        dt = Userbll. Selectleveluser (user)   ' now directly calls B-layer        datagridview.allowusertoaddrows = False   ' To remove the last line of empty lines, each time the query runs before the empty line        ' The query to that level is displayed to the user in DataGridView        If dt. Rows.Count > 0 Then            ' datagridview.rows (0). Cells ("User ID"). Value = dt. Rows (0) ("username")            ' datagridview.rows (0). Cells ("username"). Value = dt. Rows (0) ("name")            ' datagridview.rows (0) '. Cells ("Account holder"). Value = dt. Rows (0) ("Account holder")        Else            ' did not find content when            DataGridView.Rows.Clear ()            MsgBox ("No Records found!") ")        End if</span>
This Liangdang dt. Rows.Count > 0 After the description of the query to the user record, but no matter how many records found. When the DataTable returns a found user table to the U-layer, the next code is the record of the first row displayed. So it was here that made a mistake.

See some people's blog this is written. It may be that there are fewer records to join at the beginning.

every DataTable The returned data are all queries to a table, but only the DataTable one of the first lines of the initiative assigned to the front DataGridView a column, which is not wrong when the data has only one row, but the number of rows of data is more than the error.

This is because there is no loop or nesting to add data from each row to the DataGridView .

WORKAROUND: Use the loop nesting statement to assign the data returned to the DataTable table to the DataGridView table.

       <span style= "FONT-SIZE:14PX;" >       If dt. Rows.Count > 0 Then            ' datagridview.rows (0). Cells ("User ID"). Value = dt. Rows (1) ("username")            ' datagridview.rows (0). Cells ("username"). Value = dt. Rows (1) ("name")            ' datagridview.rows (0) '. Cells ("Account holder"). Value = dt. Rows (1) ("Account holder")            ' found content display for            i = 0 to dt. Rows.count-1                DataGridView.Rows.Add ()  ' starts with no rows and columns, so add a row and a column to avoid the error occurring for                j = 0 to dt. Columns.count-1                    DataGridView (J, i). Value = dt. Rows (i). Item (j)                next J                ' datagridview.allowusertoaddrows = False    ' Assuming here, there will be a last line            next i        else            DataGridView.Rows.Clear ()            MsgBox ("No Records found!") ")        End if</span>

Of course, there is a very easy way to directly bind a data source:       

Datagridview.datasource=dt
There are simple ways to solve a complex place.


( 2 Why not click once Combo will there be a lot more empty lines in the options?

The basic problem is to conquer, but when you click on the admin level in combo , the data you find is correct. But there were so many empty lines. It is not very comfortable in people's mind.



This is due to the fact that the rows in the DataGridView are not cleaned up before each query, which is a code:

<span style= "FONT-SIZE:14PX;" >datagridview.rows.clear () </span>

( 3 Why does the last behavior show up every time that there is a blank line?

The problem is solved one by one, and the problem is found in one.

When a large area of empty lines is removed, the last of the other small tail empty line. With my character it must be removed.

This is because the DataGridView control, which uses the default property, and the last empty line is a default property, how do you get rid of it?

How to resolve:

<span style= "FONT-SIZE:14PX;" >datagridview.allowusertoaddrows = False  </span>
after removing the empty line. When all the problems are resolved:

(4) Other operation of DataGridView

Delete the selected row, then delete the user, delete the DataGridView table row at the same time to delete the data in the database.


Select multiple lines of DataGridView and then operate

Delete all the lines of DataGridView and delete the user:

<span style= "FONT-SIZE:14PX;" >if introws > 0 Then for intdelrow = introws to 1 Step-1 ' from bottom to top to prevent leaking rows struseri D = Datagridview.selectedrows (intDelRow-1). Cells ("User ID"). Value.tostring () ' assumes that the user is working. You cannot delete Dim s as String s = FrmLogin.txtUserID.Text () If FrmLogin.txtUserID.Tex t = Struserid.trim () Then ' If struserid.trim () = FrmLogin.txtUserName.Text then MSGB Ox ("The user is working. Cannot delete ", vbOKOnly + vbexclamation," hint ") Exit Sub Else user. UserID = strUserID ' The user ID to be deleted is passed to entity Intresult = DELETEBLL. DeleteUser (user) ' Removes the entity's information from the database at the same time ' Intresult = Deletefac.                        DeleteUser (enuser) If intresult > 0 Then ' delete the corresponding information in the database is also deleted in the DataGridView table                    MsgBox ("Delete Failed", vbOKOnly + vbexclamation, "hint")Else DataGridView.Rows.RemoveAt (Datagridview.selectedrows (intDelRow-1). Index) MsgBox ("Delete succeeded", vbOKOnly + vbexclamation, "hint") End If End If Next Else DataGridView.Rows.Clear () End if</span>

(5) Other frequently used operations of DataGridView:

For example: Gets the index of the row of the current cell, starting at 0

(DataGridView1.CurrentCell.RowIndex). ToString ()
The hiding of the ranks:

The first column of DataGridView1 hides Datagridview1.columns[0]. Visible = false;//DataGridView1 The first line hides datagridview1.rows[0]. Visible = false;


This is just my understanding of DataGridView's current operations, and there are a lot of ways to use DataGridView frequently:

Http://blog.sina.com.cn/s/blog_97b9841901011u0u.html


Http://www.cnblogs.com/wintalen/archive/2010/12/13/1904536.html

Summary: The DataGridView control is often used when the machine room fee system is reconstructed. So it's important to master the use of the good one control, which is the equivalent of a basket that carries what we need to be visible to the user. Display the data that needs to be queried on this basket. Let the user clear information. And it is very convenient to do the operation of the data.

datagridview--administrator-to-user point of Operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.