在按钮单击中填充gridview [英] filling gridview in button click

查看:67
本文介绍了在按钮单击中填充gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在按钮中点击我使用datatable作为数据源填充gridview,并在gridview的单元格中添加按钮。



这里的问题是:



当我点击按钮

1)gridview数据应该刷新。

gridview,Refresh();

不爽。



2)

i可以更改gridview的值,然后点击相应行中的按钮,该行应该在datsbase中更新

In button click am filling gridview using datatable as datasource,and also am adding button in cell of gridview.

here the problem is:

when i click button
1) gridview data should be refreshed.
gridview,Refresh();
is not refreshing .

2)
i can change the values of gridview ,and click the button in corresponding row, that row should be updatd in datsbase also

<pre lang="c#"> private void btnedittxok_Click(object sender, EventArgs e)
        {
//            this.dataGridView1.EndEdit();

//this.dataGridView1.Refresh();
           
            if (txtedittxcustomer.Text != "")
            {
                String date1 = dateTimePicker1.Text;
                String[] date = Regex.Split(date1, "[/: ]");
                StringBuilder sb = new StringBuilder();
                sb.Append(date[2]);
                sb.Append("-");
                sb.Append(date[0]);
                sb.Append("-");
                sb.Append(date[1]);
                String fromdate = sb.ToString();


                String date2 = dateTimePicker2.Text;
                String[] date3 = Regex.Split(date2, "[/: ]");
                StringBuilder sb1 = new StringBuilder();
                sb1.Append(date3[2]);
                sb1.Append("-");
                sb1.Append(date3[0]);
                sb1.Append("-");
                sb1.Append(date3[1]);
                String todate = sb1.ToString();

                String customer = txtedittxcustomer.Text;
                String card = txtedittxcard.Text;


                using (MySqlConnection con = new MySqlConnection(ConnectionString))
                {
                    query = "select * from transaction where DATE(paid_date)BETWEEN '" + fromdate + "'AND'" + todate + "' AND customer_id='" + customer + "' ";
                    con.Open();
                    da = new MySqlDataAdapter(query, con);

                   //da.Fill(ds);
                    da.Fill(dt);
                    dataGridView1.DataSource = dt;
                    int dtRows = dt.Rows.Count;
                    if (dtRows == 0)
                    {
                        MessageBox.Show("No Records");
                    }
                    else
                        groupBox2.Visible = true;
                    //dataGridView1.DataMember = ds.Tables[0].ToString();
                    DataGridViewButtonColumn btnEdit = new DataGridViewButtonColumn();
                    dataGridView1.Columns.Add(btnEdit);
                    btnEdit.HeaderText = "Edit";
                    btnEdit.Text = "Edit";
                    btnEdit.Name = "btnEdit";
                    btnEdit.UseColumnTextForButtonValue = true;

                }
              

            }
            else
                MessageBox.Show("Please enter Customer ID");
        }

推荐答案

在更新按钮中点击活动,



首先将新值更新到数据库然后调用加载GridView的方法,它会将数据更新到数据库并在网格中反映更新的数据



在btnUpdate_Click事件中,





In the Update button Click Event,

First Update the new values to the DB and then call the method that loads the GridView, it will update the data to the DB and reflect your updated data in the Grid as well

In btnUpdate_Click Event,


{
// Method for updating new values to DB
   UpdateDB();

//Method to Load GridView()
FillGrid();

}


这篇关于在按钮单击中填充gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆