你的代码呢,没代码怎么分析?

解决方案 »

  1.   

    grid[form4.cur_row,form4.cur_col]是我在主窗口中动态生成的获得焦点的textbox
    /// <summary>
    /// 得到已经选择的表名,添写在listbox1中
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void DataBing_Load(object sender, System.EventArgs e)
    {
    this.listBox2.SelectionMode=SelectionMode.One;
    this.listBox1.SelectionMode=SelectionMode.One;

    DataTable schemaTable=new DataTable();
    string strConn = "Provider=SQLOLEDB;Data Source=D2000;Initial Catalog=cl;Trusted_Connection=Yes;Connect Timeout=11;";
        conn= new OleDbConnection(strConn);
    try
    {
    conn.Open();
    schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
    new object[] {null, null, null, "TABLE"});
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }

    for(int i=0; i < schemaTable.Rows.Count; i++)
    {
    this.listBox1.Items.Add(schemaTable.Rows[i]["TABLE_NAME"].ToString());
    }
    }
            /// <summary>
            /// 取得对应表的所有字段
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
    private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    this.listBox2.Items.Clear();
        tableName=this.listBox1.SelectedItem.ToString();
    try
    {
    string sql = "SELECT * FROM " + tableName;
    OleDbDataAdapter  comm=new OleDbDataAdapter(sql, conn);
    comm.Fill(form4.dataSet,tableName);
    form4.tempDataSet=form4.dataSet.Clone();//取得同样的架构为后面的绑定做准备
    for(int i=0; i < form4.dataSet.Tables[tableName].Columns.Count; i++)
    {
    this.listBox2.Items.Add(form4.dataSet.Tables[tableName].Columns[i].ToString());
    }
    }
    catch(Exception ex1)
    {
    MessageBox.Show(ex1.Message);
    }
    finally
    {
    conn.Close();
    }
    }
                      //双击来实现绑定
    private void listBox2_DoubleClick(object sender, System.EventArgs e)
    {
    string dataMember=this.listBox2.SelectedItem.ToString();
    form4.grid[form4.cur_row,form4.cur_col].DataBindings.Add(new Binding("Text",form4.tempDataSet,tableName+"."+dataMember));
    form4.grid[form4.cur_row,form4.cur_col].DataBing=tableName + "." + dataMember; 
    this.Close();
    }