C # datagridviewcomboxcolumn Data Binding and value display

Source: Internet
Author: User

For a C # cainiao like me, I don't know anything. I have to search for information on the Internet. It's very difficult to learn 1.1 million drops, therefore, you can record some of your questions during your learning process to facilitate your review and other colleagues' learning.

In a datagridview datagri, there is a column that I want to make into a drop-down list, so that users can finally solve the problem from the selection of a value, and record it as follows:

The following functions are available:

After opening this permission management interface, I can change the user's role (the role is already set in another interface). When I select a role or owner on the left, the corresponding user name and role name are displayed in the corresponding textbox on the top right;

Data is a table:

User table: user (userid, username)

Role table: Role (roleid, rolename)

User-role entity relationship: a user belongs to only one role, and a role has multiple users.

Open the column editor in the datagridview as follows:

Set columntype attributes, for example, to set displaystyle to dropdownbutton (in this case, only the drop-down is allowed, but the user cannot enter it). It is important to set the readonly attribute to false. Otherwise, the user cannot select the bound Data !! If no default value is set, the user cannot see or change the data! I have been entangled in this issue for a long time!

The related code is as follows:

// Bind role information to the datagridviewcomboxcolumn column. The column name is rolename.
Rolemanage roleman = new rolemanage ();
Dataset roledataset = roleman. getroleinfo (""); // get role information
Rolename. datasource = roledataset. Tables [0]; // rolename is the name of the role column in The dview. Set Data Source
Rolename. displaymember = "rolename"; // set the data source of the formatted value, that is, the drop-down text displayed by the user comes from the columns in the database table. Here, the database table column name is rolename.

Rolename. valuemember = "roleid"; // you can specify the data source that corresponds to the actual value of the drop-down text.

Bind user information to the datagridview. The datapropertyname attribute has been set in the corresponding column of the datagridview. The attribute value is the same as the column name in the database table. After binding the data source, datagridview can display corresponding columns in the database.

Usermanage userman = new usermanage ();
Dataset userdataset = userman. getuserinfo (""); // get user information

Urprelationdatagridview. datasource = userdataset. Tables [0]; // set the data source

 

Display default roles of users

Int roleidtmp;
Userrolepowerrelationmanage urprman = new userrolepowerrelationmanage ();
For (INT I = 0; I <urprelationdatagridview. Rows. Count; I ++)
{
Roleidtmp = urprman. getrolebyuserid (convert. toint32 (urprelationdatagridview. rows [I]. cells ["userid"]. value. tostring (); // determines the role of the user. If no role is assigned to the user, the role ID is-1.
Urprelationdatagridview. rows [I]. cells ["roleid"]. value = roleidtmp. tostring (); // display the role ID of the user in the role ID column of the datagridview. The column name is roleid.
If (roleidtmp! =-1) // if no role has been assigned to the user, leave the role name blank.
{
Urprelationdatagridview. rows [I]. cells ["rolename"]. value = roleidtmp; // select the role name to which the user belongs (the value of this cell is the role ID and the role name is displayed). Here, the default text of the datagridviewcomboxcolumn is set. (If the actual value of the cell is selected, the corresponding text is automatically displayed, that is, there is a ing process here)
// MessageBox. Show (urprelationdatagridview. Rows [I]. cells ["rolename"]. formattedvalue. tostring ());

// Here, urprelationdatagridview. Rows [I]. cells ["rolename"]. formattedvalue. tostring () is the text displayed in the current datagridviewcomboxcolumn (here it is the rolename column !! For other non-drop-down columns of the datagridview, the value of the cell is the same as the formatted value (displayed to the user) (formattedvalue ).
}
} // For (INT I = 0; I <urprelationdatagridatagri. rows. Count; I ++)

The following code displays the role name of the selected row in the textbox on the right. Several events are required:

// Occurs when the current cell in the datagridview control changes or the control receives the input focus.
// If the control has no focus input and the clicked cell is not the current cell, this event may appear twice in one click.
Private void urprelationdatagridview_cellenter (Object sender, datagridviewcelleventargs E)
{
If (urprelationdatagridview. currentrow! = NULL)
{
Object formattedvalue = urprelationdatagridview. currentrow. cells ["rolename"]. formattedvalue;
Object value = urprelationdatagridview. currentrow. cells ["rolename"]. value;
If (formattedvalue! = NULL & value! = NULL)
{
// Int roleidtmp = convert. toint32 (urprelationdatagridatagri. currentrow. cells ["rolename"]. value. tostring (). Trim (); // obtain the currently selected role ID
Rolenametextbox. Text = urprelationdatagridatagri. currentrow. cells ["rolename"]. formattedvalue. tostring ();
}
Else
{
Rolenametextbox. Text = "";
}
}
} // Private void urprelationdatagridview_cellenter (Object sender, datagridviewcelleventargs E)

 

// This event occurs when the user-specified value is submitted. The user-specified value is usually submitted when the focus leaves the cell.
Private void urprelationdatagridview_cellvaluechanged (Object sender, datagridviewcelleventargs E)
{
If (urprelationdatagridview. currentrow! = NULL)
{
If (urprelationdatagridview. currentrow. cells ["rolename"]. formattedvalue! = NULL)
{
Rolenametextbox. Text = urprelationdatagridatagri. currentrow. cells ["rolename"]. formattedvalue. tostring ();
// Urprelationdatagridview. currentrow. cells ["roleid"]. value = urprelationdatagridview. currentrow. cells ["rolename"]. value;
}
}
} // Private void urprelationdatagridview_cellvaluechanged (Object sender, datagridviewcelleventargs E)

 

// When the cell content has been changed but the changes have not been saved, the cell is marked as modified.
// This event usually occurs when the cell has been edited but the change has not been submitted to the data cache, or when the edit operation is canceled.
Private void urprelationdatagridview_currentcelldirtystatechanged (Object sender, eventargs E)
{
If (urprelationdatagridview. iscurrentcelldirty) // The iscurrentcelldirty value indicates whether the current cell has uncommitted changes. If yes, it is true.
{
Urprelationdatagridview. commitedit (datagridviewdataerrorcontexts. Commit); // submit the changes in the current cell to the data cache, but do not end the editing mode. This will trigger the cellvaluechanged event.
}
}

 

Reference: http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridviewcell.formattedvalue.aspx

Http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridview_events (V = vs.80). aspx

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.