namespace app
{
/// <summary>
/// Form4 的摘要说明。
/// </summary>
public class frm_user : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private DataSet myDataSet,myDataSet_group;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public frm_user()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//设计DataGrid 形式为combox
MakeComboxGrid();
//
dataGrid1.DataSource=myDataSet.Tables["RightUser"];
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
private void MakeComboxGrid()
{
//制造数据集////////////
MakeDataSet();

//制造列形式//////
AddTableStyle();


}
private void MakeDataSet()
{
///////////////////////创建用户信息数据集
SqlConnection myConn = new SqlConnection(LoginData.ConnectString);
string selectstring ="SELECT userid,username,password,rightgroup from U_RIGHTUSER ";
SqlCommand myComm = myConn.CreateCommand();
myComm.CommandText=selectstring;
SqlDataAdapter myAdap = new SqlDataAdapter();
myAdap.SelectCommand = myComm;
//生成数据集
myConn.Open();
string DataTableName ="RightUser";
myDataSet = new DataSet("myDataSet");
myAdap.Fill(myDataSet,DataTableName);
myConn.Close();
//DataTable myDataTable = myDataSet.Tables[DataTableName];
////////////创建组权限数据集/////////
SqlConnection myConn_group = new SqlConnection(LoginData.ConnectString);
string selectgroupstring = "SELECT group_id,group_name,group_text from U_RIGHTGROUP";
SqlCommand myComm_group = myConn_group.CreateCommand();
myComm_group.CommandText = selectgroupstring;
SqlDataAdapter myAdap_group = new SqlDataAdapter();
myAdap_group.SelectCommand = myComm_group;
myConn_group.Open();
string DataTableName_group = "RightGroup";
myDataSet_group = new DataSet("myDataSet_group");
myAdap_group.Fill(myDataSet_group,DataTableName_group);
myConn_group.Close();
}
private void AddTableStyle()
{
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "RightUser";
// Set other properties.
ts1.AlternatingBackColor = Color.LightGray;
//
// Add 2 cols with textbox column style
DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "userid";
TextCol.HeaderText = "用户编号";
TextCol.ReadOnly = true;
TextCol.Width = 120;
ts1.GridColumnStyles.Add(TextCol); TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "username";
TextCol.HeaderText = "用户姓名";
TextCol.ReadOnly = true;
TextCol.Width = 120;
ts1.GridColumnStyles.Add(TextCol); TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "password";
TextCol.HeaderText = "用户密码";
TextCol.TextBox.PasswordChar = '*';
TextCol.ReadOnly = true;
TextCol.Width = 120;
ts1.GridColumnStyles.Add(TextCol);
//*******************************************// the main idea
// Step 2 - Use the combo column style 
// Add 1 col with combo style
DataGridComboBoxColumn ComboTextCol = new DataGridComboBoxColumn(new ComboValueChanged(MyComboValueChanged));
ComboTextCol.MappingName = "rightgroup";
ComboTextCol.HeaderText = "用户权限";
ComboTextCol.Width = 200;
ts1.GridColumnStyles.Add(ComboTextCol);
ts1.PreferredRowHeight = ComboTextCol.ColumnComboBox.Height + 3;
ComboTextCol.ColumnComboBox.DataSource = myDataSet_group.Tables["RightGroup"];
ComboTextCol.ColumnComboBox.DisplayMember = "group_name";
ComboTextCol.ColumnComboBox.ValueMember = "group_id";
//ComboTextCol.ColumnComboBox.Items.Clear();
//ComboTextCol.ColumnComboBox.Items.Add("Chicago");
ComboTextCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
dataGrid1.TableStyles.Add(ts1);
} public void MyComboValueChanged(int rowChanging, object newValue)
{
Console.WriteLine("index changed {0} {1}", rowChanging, newValue);
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// 

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
// 
// dataGrid1
// 
this.dataGrid1.DataMember = "";
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(592, 273);
this.dataGrid1.TabIndex = 0;
// 
// frm_user
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(592, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
  this.dataGrid1});
this.Name = "frm_user";
this.Text = "操作员录入";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion
}
}