---------------------------------------------
|                                            |
|                                            |
|                                            |
|          上半部分是输入界面                |
|                                            |
|                                            |
----------------------------------------------
|                                            |
|                                            |
|           下半部分是DataGrid显示           |
|                                            |
|                                            |
|---------------------------------------------写WEB久了,现在写winform,每个输入要作成一个WINFORM界面,如上:上半部分是输入界面 ,下半部分是DataGrid显示。每次添加新数据后,datagrid 如何刷新(把新的显示出来呢?)

解决方案 »

  1.   

    构造函数
    public ColdcodeCfg()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();
    this.ds = link.SelectDataBase(sendStrSQL,sendTableName);//创建数据库连接
    this.dgrd_ColdcodeCfg.DataSource = ds;  //这是绑定了吧
    this.dgrd_ColdcodeCfg.DataMember = myTableName; lbxKey_Pos.SelectedIndex=0;
    lbxKey_Value.SelectedIndex=0;
    lbxPos_One.SelectedIndex=0;
    lbxPos_Two.SelectedIndex =0;
    lbxPos_Three.SelectedIndex=0; }下面是点上面的修改界面的动作:// 更新为新的数据
    private void btnSubmitCfg_Click(object sender, System.EventArgs e)
    {
    try
    {
    //更新数据到数据库 
    string intKey_Pos = lbxKey_Pos.SelectedItem.ToString(); 
    string intKey_Value = lbxKey_Value.SelectedItem.ToString(); 

    string intAdd_Value =  txtAdd_Value.Text.Trim();

    string strPos_One = lbxPos_One.SelectedItem.ToString(); 
    string strPos_Two = lbxPos_Two.SelectedItem.ToString(); 
    string strPos_Three = lbxPos_Three.SelectedItem.ToString(); 

    int intTotal_Coldcode = Int32.Parse(txtTotal_Coldcode.Text.Trim());  decimal decUp_Limit = Convert.ToDecimal(txtUp_Limit.Text.Trim());
    decimal decLow_Limit  = Convert.ToDecimal(txtLow_Limit.Text.Trim());
    DateTime dtSubmit_Time = DateTime.Today;

    string sendSQL = "update ColdCode_Cfg set ";
    if ((intKey_Pos != "无") && (intKey_Value != "无"))
    {
    sendSQL += " Key_Position = '"+ intKey_Pos +"',";
    sendSQL += " Key_Value = '"+ intKey_Value +"',";
    }
    else
    {
    sendSQL += " Key_Position = '',";
    sendSQL += " Key_Value = '',";
    }
    if (intAdd_Value != null)
    {
    sendSQL += " Add_Value = '"+ intAdd_Value +"',";
    }
    else
    {
    sendSQL += " Add_Value = '',";
    }
    if ((strPos_One != "无") && (strPos_Two != "无") && (strPos_Three != "无"))
    {
    sendSQL += " Pos_One = '"+ strPos_One +"',";
    sendSQL += " Pos_Two = '"+ strPos_Two +"',";
    sendSQL += " Pos_Three = '"+ strPos_Three +"',";
    }
    else
    {
    sendSQL += " Pos_One = '',";
    sendSQL += " Pos_Two = '',";
    sendSQL += " Pos_Three = '',";
    }
    sendSQL += " Total_Coldcode_Times = '"+ intTotal_Coldcode +"',";
    sendSQL += " Up_Limit = '"+ decUp_Limit +"',";
    sendSQL += " Low_Limit = '"+ decLow_Limit +"',"; sendSQL += " Submit_Time = '"+ dtSubmit_Time +"'";
    if (rdb3D.Checked)
    {
    sendSQL += " where id = 1";
    }
    else if(rdbP3.Checked)
    {
    sendSQL += " where id = 2";
    }
    else
    {
    MessageBox.Show("请选择类别!!!","提示");
    return;
    } this.link.UpdateDataBase(sendSQL); 

    MessageBox.Show("条件设置成功!","信息");

    //ds.Clear();
    //this.ds = link.SelectDataBase(sendStrSQL,sendTableName);//创建数据库连接
    //this.dgrd_ColdcodeCfg.DataSource = ds.Tables[0];
             //如果上面3行不注销,当然可以显示新数据,这样的话又连接数据库,好慢啊


    catch
    {
    MessageBox.Show("数据保存失败,请确认所有信息输入完整且正确!","提示");
    return;
    }
    }如果下面3行不注销,当然可以显示新数据,这样的话又连接数据库,好慢啊
    //ds.Clear();
    //this.ds = link.SelectDataBase(sendStrSQL,sendTableName);//创建数据库连接
    //this.dgrd_ColdcodeCfg.DataSource = ds.Tables[0];有什么方法解决呢?
      

  2.   

    按钮提交的时候处罚datagrid的bind事件