Click here to Skip to main content
16,020,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Once below code obser I am writing this code in one page
C#
    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

C#
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);
          }
      }

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
Posted
Updated 2-Dec-10 23:29pm
v5

1 solution

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();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900