如何刷新gridview数据,数据从另一个页面更新 [英] How To refresh gridview data ,data is updated from another page

查看:154
本文介绍了如何刷新gridview数据,数据从另一个页面更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码观察者中,我会在一页中编写此代码

Once below code obser I am writing this code in one page

    public partial class RadioCapture : Form
    { 
        SqlConnection con = new SqlConnection("Server=SYSTEM3\\SQLEXPRESS;User id=sa;password=123;database=videocapture");
        SqlDataAdapter da;
        DataSet ds;
        SqlCommand cmd;
        public RadioCapture()
        {
            InitializeComponent();
        }
private void RadioCapture_Load(object sender, EventArgs e)
    {
        Getdata();
    }

        public void Getdata()
        {
            cmd = new SqlCommand("select * from PatientDetails", con);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "PatientDetails");
            da.FillSchema(ds, SchemaType.Source, "PatientDetails");
            SqlCommandBuilder bldr = new SqlCommandBuilder(da);
            dataGridView1.DataSource = ds.Tables[0];         
        }
        private void BtnNew_Click(object sender, EventArgs e)
        {
            PatientDetails pd = new PatientDetails();
            pd.Show();
        }
      
        private void btnModify_Click(object sender, EventArgs e)
        {
            try
            {
                PatientDetails pd = new PatientDetails();
                int i = dataGridView1.CurrentRow.Index;
                pd.PId = dataGridView1.Rows[i].Cells[0].Value.ToString();
                pd.PName = dataGridView1.Rows[i].Cells[1].Value.ToString();
                pd.PAge = dataGridView1.Rows[i].Cells[2].Value.ToString();
                pd.Pprocedure =      dataGridView1.Rows[i].Cells[4].Value.ToString();
                pd.PDrName = dataGridView1.Rows[i].Cells[5].Value.ToString();
                pd.PDateTime = dataGridView1.Rows[i].Cells[6].Value.ToString();
                pd.Show();
            }
            catch
            { 
            MessageBox.Show("Please select one Record...");
            }
        }
        private void BtnRecordDelete_Click(object sender, EventArgs e)
        {
            try
            {
                  MessageBox.Show("Do you want delete...");
                  int i = dataGridView1.CurrentRow.Index;
                  string s= dataGridView1.Rows[i].Cells[0].Value.ToString();
                  string s1 = "delete PatientDetails where PatientId='" + s + "'";
                  cmd = new SqlCommand(s1, con);
                  cmd.CommandType = CommandType.Text;
                  con.Close();
                  con.Open();
                  cmd.ExecuteNonQuery();
                  con.Close();
                  MessageBox.Show("record is deleted ...");
                  Getdata();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
}}


另一页我写了以下代码


Another Page I wrote following code

SqlConnection con = new SqlConnection("Server=SYSTEM3\\SQLEXPRESS;User id=sa;password=123;database=videocapture");
SqlCommand cmd;
 public string PId
 {
    get { return TxtPatientId.Text; }
    set { TxtPatientId.Text = value; }
 }
 public string PName
 {
     get { return TxtPatientName.Text; }
     set { TxtPatientName.Text = value; }
 }
 public string PAge
 {
     get { return TxtAge.Text; }
     set { TxtAge.Text = value; }
 }
 public string Pprocedure
 {
     get { return TxtProcedure.Text; }
     set { TxtProcedure.Text = value; }
 }
 public string PDrName
 {
     get { return TxtDrName.Text; }
     set { TxtDrName.Text = value; }
 }
 public string PDateTime
 {
     get { return TxtDateTime.Text; }
     set { TxtDateTime.Text = value; }
 }
 public PatientDetails()
 {
    InitializeComponent();
 }
private void BtnUpdate_Click(object sender, EventArgs e)
      {
          try
          {
             string s1;
              if (RbtnMale.Checked)
              {
                  s1 = RbtnMale.Text;
              }
              else
              {
                  s1 = RbtnFemale.Text;
              }
              string s = "update PatientDetails set PatientName='" + TxtPatientName.Text + "',Age='"+TxtAge.Text+"',Sex='"+s1.ToString()+"',Procedur='"+ TxtProcedure.Text +"',DrName='"+TxtDrName.Text+"',DateTime='"+TxtDateTime.Text+"' where PatientId='" + TxtPatientId.Text + "'";
              cmd = new SqlCommand(s, con);
              cmd.CommandType = CommandType.Text;
              con.Close();
              con.Open();
              cmd.ExecuteNonQuery();
              con.Close();
              MessageBox.Show("record is updated into database...");
              RadioCapture obj = new RadioCapture();
              obj.Getdata();
          }
          catch (SqlException ex)
          {
              MessageBox.Show(ex.Message);
          }
      }


当点击删除按钮时,它将被删除&立即将记录从datagridview中删除,但是我单击了更新的按钮,但数据已更新,但未立即显示在datagridview中,为什么?
但是我在两个按钮中调用时都单击了相同的函数getdata();
我想立即在datagridview上显示更新的数据.
请告诉我过程
请给出适当的答案,它不是Asp.Net中的gridview
它是c#.net


when click on delete button it will be deleted & immediately the record is deleted from datagridview but i click on updated button data is updated but it is not displayed immediately in datagridview why ?
But i called in both buttons click same function getdata();
I want display updated data immediately on datagridview.
please tell me process
please give suitable answer it is not gridview in Asp.Net
it is datagridview in c#.net

推荐答案

简单的下面的代码来刷新网格视图的datagridview ...

类s1 = new class();
gridview name.datasource = s1.getdate(从表名中选择*");
网格视图名称.databind();
simple write the below code to refresh the grid view...

Class s1=new class();
gridview name.datasource=s1.getdate("select * from TABLE-NAME");
grid view name.databind();


这篇关于如何刷新gridview数据,数据从另一个页面更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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