Click here to Skip to main content
15,946,988 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i need solution for repeat items in DataGridView
i design form for Patients , i try to show Patient List in dataGridView
But when i closed form and open DataGridView For Patient Again , Patient List is repeating
for Example
i have 10 row for Patients in database
when i open form DataGridView show 10 row

But when i closed form and open DataGridView For Patient Again datagridview
show 10 row and repeat the same row again

What I have tried:

class to get data from database
C#
public static  class Show_Patients_In_DataGridView
    {
      static  DXL.DXL ob = new DXL.DXL();

     
        public static DataTable View_Patient_Data()

        {
            DataTable dt = new DataTable();
            ob.open();
            dt = ob.Reader("View_All_Patients", null);

            ob.close();
            return dt;


        }


    }




    public partial class Patient_Management : Form
    {
        public Patient_Management()
        {
            InitializeComponent();
            DGV.DataSource = Show_Patients_In_DataGridView.View_Patient_Data();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            DGV.DataSource = null;
        }

        private void DGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DGV.DataSource = Show_Patients_In_DataGridView.View_Patient_Data();
        }
    }
Posted
Updated 14-Mar-22 12:26pm
v2
Comments
Maciej Los 10-Mar-22 15:25pm    
What's an issue?
Richard Deeming 11-Mar-22 3:07am    
There's most likely a bug in your custom DXL class, which you haven't shown.
ahmedbelal 11-Mar-22 9:58am    
namespace TheClinic.DXL
{
public class DXL
{


SqlCommand cmd;
SqlDataAdapter da;
SqlConnection cn;
DataTable dt = new DataTable();

public DXL() /* باني افتراضي لاجل بناء اتصال */
{
cn = new SqlConnection(@"server=DESKTOP-VTLRB7T;database=DataBase_Clinic ;integrated security=true");
}

public void open()
{
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}
}

public void close()
{
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
}
//تابع لقراءة البيانات من قاعدة البيانات
public DataTable Reader(string sp, SqlParameter[] p)
{
cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = sp;
cmd.Connection = cn;
if (p != null)
{
cmd.Parameters.AddRange(p);
}
da = new SqlDataAdapter(cmd);
da.Fill(dt);
return dt;


}
//Remove ,Update ,Add
public void RUA(string sp, SqlParameter[] p)
{
cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = sp;
cmd.Connection = cn;
if (p != null)
{
cmd.Parameters.AddRange(p);
}
cmd.ExecuteNonQuery();

}








}
}

1 solution

C#
private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DGV.Rows.Clear();
            DGV.DataSource = null;
        }
 
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