先说需求,利用DataGrid动态生成含有ChecBox和ComBox;做类似SQl2000中创建新表的窗体
现在DataGrid样式已经达到要求;问题如下
怎么在点击comBox时触发事件?
怎么获得各行的值,拼接成SQL创建表格的语句?
如果是修改表结构,是不是直接将含有表结构的dataTable付给dataGrid就行?
或者有其他好的办法

麻烦大侠门出来帮小弟一下
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
 
namespace DataGridTest
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dgdFunctionArea;
private DataTable dtblFunctionalArea;
private System.Windows.Forms.Button buttonFocus;
private System.Windows.Forms.TextBox textBox1;
private System.ComponentModel.Container components = null;
 
public Form1()
{
InitializeComponent();
PopulateGrid();
}
 
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
}
 
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.dgdFunctionArea = new System.Windows.Forms.DataGrid();
this.buttonFocus = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.dgdFunctionArea)).BeginInit();
this.SuspendLayout();
// 
// dgdFunctionArea
// 
this.dgdFunctionArea.AlternatingBackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(255)));
this.dgdFunctionArea.BackgroundColor = System.Drawing.SystemColors.ActiveBorder;
this.dgdFunctionArea.CaptionBackColor = System.Drawing.Color.LightSteelBlue;
this.dgdFunctionArea.DataMember = "";
this.dgdFunctionArea.HeaderForeColor = System.Drawing.SystemColors.Desktop;
this.dgdFunctionArea.Location = new System.Drawing.Point(48, 40);
this.dgdFunctionArea.Name = "dgdFunctionArea";
this.dgdFunctionArea.SelectionBackColor = System.Drawing.SystemColors.Desktop;
this.dgdFunctionArea.Size = new System.Drawing.Size(316, 168);
this.dgdFunctionArea.TabIndex = 0;
// 
// buttonFocus
// 
this.buttonFocus.Location = new System.Drawing.Point(192, 240);
this.buttonFocus.Name = "buttonFocus";
this.buttonFocus.Size = new System.Drawing.Size(84, 23);
this.buttonFocus.TabIndex = 1;
this.buttonFocus.Text = "获取焦点";
this.buttonFocus.Click += new System.EventHandler(this.buttonFocus_Click);
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(120, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(240, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "textBox1";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(448, 277);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.buttonFocus);
this.Controls.Add(this.dgdFunctionArea);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dgdFunctionArea)).EndInit();
this.ResumeLayout(false); }
#endregion
 
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}
//初始化DataGrid
private void PopulateGrid()
{
//创建一个DataTable对象,包括四列,前三列为String,最后一列为Boolean。
dtblFunctionalArea = new DataTable ("FunctionArea");
string[] arrstrFunctionalArea = new string [3]{"列名","数据类型","长度"};
DataColumn dtCol = null;
//创建String列       
for(int i=0; i< 3;i++)
{    
dtCol = new DataColumn(arrstrFunctionalArea[i]);
dtCol.DataType = Type.GetType("System.String");
dtCol.DefaultValue = "";
dtblFunctionalArea.Columns.Add(dtCol);               
}     
 
//创建Boolean列,用CheckedBox来显示。    
DataColumn dtcCheck = new DataColumn("允许空");
dtcCheck.DataType = System.Type.GetType("System.Boolean");
dtcCheck.DefaultValue = false;
dtblFunctionalArea.Columns.Add(dtcCheck);
 
//把表绑定到DataGrid
dgdFunctionArea.DataSource    = dtblFunctionalArea; 
 
//为DataGrid加载DataGridTableStyle样式
if(!dgdFunctionArea.TableStyles.Contains("FunctionArea"))
{
DataGridTableStyle dgdtblStyle = new DataGridTableStyle();
dgdtblStyle.MappingName = dtblFunctionalArea.TableName;
dgdFunctionArea.TableStyles.Add(dgdtblStyle);
dgdtblStyle.RowHeadersVisible = true;
// dgdtblStyle.HeaderBackColor = Color.LightSteelBlue;
dgdtblStyle.AllowSorting = false;
// dgdtblStyle.HeaderBackColor = Color.FromArgb(8,36,107);
// dgdtblStyle.HeaderForeColor = Color.White;
dgdtblStyle.HeaderFont = new System.Drawing.Font("Microsoft Sans Serif", 9F, 
System.Drawing.FontStyle.Bold, 
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
dgdtblStyle.GridLineColor = Color.DarkGray;
dgdtblStyle.PreferredRowHeight = 22;
dgdFunctionArea.BackgroundColor = Color.White; 
 
//设置列的宽度 
GridColumnStylesCollection colStyle = dgdFunctionArea.TableStyles[0].GridColumnStyles;
colStyle[0].Width = 100;
colStyle[1].Width = 50;
colStyle[2].Width = 50;
colStyle[3].Width = 80;
}
 
DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dgdFunctionArea.TableStyles[0].GridColumnStyles[1]; 
ComboBox cmbFunctionArea = new ComboBox();
cmbFunctionArea.Items.AddRange(new object[]{"int","float","nvarchar"});
cmbFunctionArea.Cursor = Cursors.Arrow;
cmbFunctionArea.DropDownStyle= ComboBoxStyle.DropDownList;
cmbFunctionArea.Dock = DockStyle.Fill;
//在选定项发生更改并且提交了该更改后发生
cmbFunctionArea.SelectionChangeCommitted += new EventHandler(cmbFunctionArea_SelectionChangeCommitted); 
//把ComboBox添加到DataGridTableStyle的第2列
dgtb.TextBox.Controls.Add(cmbFunctionArea);         
 
}
//把Combobox上修改的数据提交到当前的网格
private void cmbFunctionArea_SelectionChangeCommitted(object sender, EventArgs e)
{
this.dgdFunctionArea[this.dgdFunctionArea.CurrentCell] = ((ComboBox)sender).SelectedItem.ToString();
}       
private void buttonFocus_Click(object sender, System.EventArgs e)
{
  string sqlstr=ProductsSql(); } private string ProductsSql()
{  string sql=null;
string tablename=this.textBox1.Text.ToString();
 for (int i=0;i<this.dgdFunctionArea.VisibleRowCount-1;i++)
 {   

     string s=null;
 string n=null;
 DataGridCell dgc0=new DataGridCell(i,0);
 DataGridCell dgc1=new DataGridCell(i,0);
 DataGridCell dgc2=new DataGridCell(i,0);
 DataGridCell dgc3=new DataGridCell(i,0);
 string c0=dgc0.ToString().Trim();//这里取值语法不对  string c1=dgc1.ToString().Trim();
 string c2=dgc2.ToString().Trim();
 MessageBox.Show(c0);
if (Convert.ToBoolean(dgc3.ToString()))
{
n="NULL";

else
{
n="NOT NULL";
}
 s=""+c0+"";
    s+=""+c0+" "+c1+"("+c2+") "+n+",";
MessageBox.Show(s);
 }
sql="CCREATE TABLE JXDX_TSCKZHFL (xdxdh int identity NOT NULL PRIMARY KEY,zhdh char(21) NULL,zzh char(20) NULL,pzdh char(20) NULL,zhcplb char(2) NULL)";
return sql;
}
}
}

解决方案 »

  1.   

    1.怎么在点击comBox时触发事件? 不晓得你要在点击 comBox 时触发什么事件,哪个东东的事件。
    如果是要触发 comBox 控件的事件 在编辑模板列的里面选它的事件啊。2.怎么获得各行的值,拼接成SQL创建表格的语句? 要获得行中控件的值,用ComBox cg=(comBox)datagrid.FindControl("控件id");来找到控件,然后用cg来取值3.如果是修改表结构,是不是直接将含有表结构的dataTable付给dataGrid就行? 之于这个问题你可以在程序中试试,好久没弄cs的东东了
      

  2.   

    1.怎么在点击comBox时触发事件? 不晓得你要在点击 comBox 时触发什么事件,哪个东东的事件。 
    如果是要触发 comBox 控件的事件 在编辑模板列的里面选它的事件啊。 
    像上sql中一样 点击数据类型例如 int ,数据长度就变成4,comBox对应的列为数据类型
    2.怎么获得各行的值,拼接成SQL创建表格的语句?
     要获得行中控件的值,用ComBox cg=(comBox)datagrid.FindControl("控件id");来找到控件,然后用cg来取值 

    c1=cg.SelectedItem.ToString();???对吗
    DataGridCell dgc0=new DataGridCell(i,0); 
    string c0=dgc0.ToString().Trim();//这里为什么不对,取第一列i行的值
    checkbox 的取值也是一样吗?
    二者赋值呢?怎么赋值
      

  3.   

    上面第一条中 comBox后面一列为数据长度,如果前面选的是int则变成4怎么让他不能在编辑,而如果是char变为可编辑
      

  4.   

    你添加 cmbFunctionArea 控件的时候,给他加上 一个事件。
    cmbFunctionArea.SelectedIndex+= new 
    怎么写的不大会了
    然后你就在这个事件里去实现 的功能
    protect SelectedIndex(sender s,ever e)
     {
    comboxt xb = (combox)sender
    if xb.selectvalue = int
       findcontrol(textbox).text =4
       findcontrol(textbox).readonly=true
    }