想要像在WinForm里面一样使用DataGrid是不可能的,你看CommandColumn里面都没有Insert。
给你个建议,添加一个简单的输入表单吧
至于你说为什么不能编辑,我想你一定是没有把OnEditCommand的事件写好
给你推荐一个地方,其实我估计你也知道
http://chs.gotdotnet.com/quickstart/default.aspx
上面有很多可以解决你困惑的地方

解决方案 »

  1.   

    VSJobs() (  ) 
    现在我给它加上了一个空行
    但是不能编辑
    你说的OnEditCommand的事件里要写些什么呢
      

  2.   

    : titicaca(枫叶) (  ) 
    不是啊
    它没有READONLY的属性
    不是WIN FORM
    是WEB
      

  3.   

    http://chs.gotdotnet.com/quickstart/default.aspx
    应该自己思考一些问题
    中文的
    言简意赅
      

  4.   

    建议你使用模板列
    <asp:TemplateColumn HeaderText="用户邮箱">
    <HeaderStyle Wrap="False"></HeaderStyle>
    <ItemStyle Wrap="False"></ItemStyle>
    <ItemTemplate>
    <asp:Label id="label1" Runat="server"></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:textbox id="TextBox1" runat="server"></asp:textbox>
    </EditItemTemplate>
    </asp:TemplateColumn>
      

  5.   

    我做了在Windows Form中做了一个例子!!
    留个Email发一个给你
      

  6.   

    luningjfy_2001(ln)
    谢了
    [email protected]
      

  7.   

    using System; 
    using System.Data; 
    using System.Data.SqlClient; 
    using System.Drawing; 
    using System.Windows.Forms; namespace MyDataGrid
    {
    public class DataGridSample:Form

    System.Windows.Forms.DataGrid myGrid; 
       
    SqlConnection con; 
    SqlDataAdapter adapter; 
    DataSet ds; 
    Button ok, cancel,Delete; 
            
    SqlParameter workParam = null; 
            
    // apply to the columns in the table 
    string query = "select * from Stud_information";
    private System.Windows.Forms.Button button1; 
            
    // change the Server ,uid, pwd and database accordingly 
    string url = "data source=JSJ117;initial catalog=Student;integrated security=SSPI;persist security info=False;workstation id=JSJ117;packet size=4096";  //数据库连接字符串 static void Main()

    Application.Run(new DataGridSample()); 
    }  public DataGridSample()

    InitializeComponent(); 
    ConnectToData();
    myGrid.SetDataBinding(ds,"Stud_information");
    }  public void InitializeComponent()

    this.myGrid = new System.Windows.Forms.DataGrid();
    this.ok = new System.Windows.Forms.Button();
    this.cancel = new System.Windows.Forms.Button();
    this.Delete = new System.Windows.Forms.Button();
    this.button1 = new System.Windows.Forms.Button();
    ((System.ComponentModel.ISupportInitialize)(this.myGrid)).BeginInit();
    this.SuspendLayout();
    // 
    // myGrid
    // 
    this.myGrid.CaptionBackColor = System.Drawing.SystemColors.Control;
    this.myGrid.DataMember = "";
    this.myGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.myGrid.Location = new System.Drawing.Point(16, 0);
    this.myGrid.Name = "myGrid";
    this.myGrid.Size = new System.Drawing.Size(500, 350);
    this.myGrid.TabIndex = 0;
    // 
    // ok
    // 
    this.ok.Location = new System.Drawing.Point(10, 375);
    this.ok.Name = "ok";
    this.ok.Size = new System.Drawing.Size(70, 30);
    this.ok.TabIndex = 1;
    this.ok.Text = "OK";
    this.ok.Click += new System.EventHandler(this.button_Click);
    // 
    // cancel
    // 
    this.cancel.Location = new System.Drawing.Point(95, 375);
    this.cancel.Name = "cancel";
    this.cancel.Size = new System.Drawing.Size(70, 30);
    this.cancel.TabIndex = 1;
    this.cancel.Text = "Cancel";
    this.cancel.Click += new System.EventHandler(this.button_Click);
    // 
    // Delete
    // 
    this.Delete.Location = new System.Drawing.Point(175, 375);
    this.Delete.Name = "Delete";
    this.Delete.Size = new System.Drawing.Size(70, 20);
    this.Delete.TabIndex = 2;
    this.Delete.Text = "Delete";
    this.Delete.Click += new System.EventHandler(this.button_Click);
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(264, 368);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(88, 24);
    this.button1.TabIndex = 3;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // DataGridSample
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(550, 450);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.button1,
      this.myGrid,
      this.ok,
      this.cancel,
      this.Delete});
    this.Name = "DataGridSample";
    this.Text = "C# DataGrid with DataSet - Example";
    ((System.ComponentModel.ISupportInitialize)(this.myGrid)).EndInit();
    this.ResumeLayout(false); } 
            
    public void ConnectToData()

    ds = new DataSet(); 
    con = new SqlConnection(url); 
    adapter = new SqlDataAdapter(); 
    adapter.SelectCommand = new SqlCommand(query, con); 
    adapter.Fill(ds, "stud_information"); 
    insertCommand(); 
    updateCommand(); 

            
    public void updateCommand() 
    {       
    string query = "Update stud_information Set Name = @Name,Class=@Class where Number=@Number";
    adapter.UpdateCommand = new SqlCommand(query, con); 
            
    workParam = adapter.UpdateCommand.Parameters.Add("@Number", SqlDbType.BigInt); 
    workParam.SourceColumn = "Number"; 
    workParam.SourceVersion = DataRowVersion.Original; 
            
    workParam = adapter.UpdateCommand.Parameters.Add("@Name", SqlDbType.VarChar); 
    workParam.SourceVersion = DataRowVersion.Current; 
    workParam.SourceColumn = "Name";  workParam = adapter.UpdateCommand.Parameters.Add("@Class", SqlDbType.VarChar); 
    workParam.SourceColumn = "Class"; 
    workParam.SourceVersion = DataRowVersion.Current;  }         
    public void button_Click(object sender, EventArgs evArgs) 

    if (sender==ok)

    UpdateValue(); // update the database once everything done. 

    else if (sender==cancel) 

    this.Dispose(); 

    else if(sender==Delete)
    {
    int index=myGrid.CurrentRowIndex;
    ds.Tables[0].Rows.RemoveAt(index);
    //UpdateValue();
    System.Data.SqlClient.SqlCommandBuilder abuilder=new System.Data.SqlClient.SqlCommandBuilder(adapter);
    adapter.Update(ds,"Stud_information");
    }
    }         
    private void Row_Changed(object ob, DataRowChangeEventArgs e) 

    DataTable t = (DataTable)  ob; 
    Console.WriteLine("RowChanged " + e.Action.ToString() + "\t" + e.Row.ItemArray[0]); 

              
    public void insertCommand() 
    {       
    string insertQuery = "Insert into stud_information VALUES (@Number, @Name, @Class)"; 
    adapter.InsertCommand = new SqlCommand(insertQuery, con); 
            
    workParam = adapter.InsertCommand.Parameters.Add("@Number", SqlDbType.BigInt); 
    workParam.SourceColumn = "Number"; 
    workParam.SourceVersion = DataRowVersion.Current; 
            
    workParam = adapter.InsertCommand.Parameters.Add("@Name", SqlDbType.VarChar); 
    workParam.SourceVersion = DataRowVersion.Current; 
    workParam.SourceColumn = "Name";  workParam = adapter.InsertCommand.Parameters.Add("@Class", SqlDbType.VarChar);
    workParam.SourceColumn = "Class"; 
    workParam.SourceVersion = DataRowVersion.Current; 
            

            
            
    public void UpdateValue() 

    try 

    adapter.Update(ds, "stud_information"); 
    Console.Write("Updating DataSet succeeded!"); 

    catch(Exception e) 

    Console.Write(e.ToString()); 

    } private void button1_Click(object sender, System.EventArgs e)
    {
    Form2 frm=new Form2();
    frm.Show();

              

    }