Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a maskedtextbox, a button and a gridview.

everytime i enter date in maskedtextbox to show data in gridview by clicking on btnshow(button)
it shows me data with the the entered date and again when i enter different date it shows me the data related to the new date entered but also the previous data...so i don't know how to refresh the gridview to show only the new data related to date enetered.
My code for show button is as follows;-

private void BTN_SHOW_Click(object sender, EventArgs e)
        {
            
            SqlDataAdapter da = new SqlDataAdapter("select dicharge_date AS 'Discharge Date',bed_id AS 'Room No',level1 AS 'Floor No',last_name AS 'Patient Name',doctor_name AS 'Patient Dr. Name',Discharge_Advice AS 'Dis Adv',Discharge_Card AS 'Dis Card',File_Check AS 'File Check',Billing,Actual_Discharge AS 'Actual Discharge',Time,reg_no,discharge_reason AS Remarks from NewDischargeTimes where dicharge_date=CONVERT(date, '" + maskedtxbx_dischargedate.Text + "', 103)", con);
            SqlCommandBuilder cmd = new SqlCommandBuilder(da);
            da.Fill(dtusers);
            dataGridView1.DataSource = dtusers;
            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            
        }
Posted
Updated 28-Sep-13 6:34am
v3
Comments
i.fakhari 28-Sep-13 9:48am    
let's see your code :-]
alanmaster 28-Sep-13 9:53am    
private void BTN_SHOW_Click(object sender, EventArgs e)
{

SqlDataAdapter da = new SqlDataAdapter("select dicharge_date AS 'Discharge Date',bed_id AS 'Room No',level1 AS 'Floor No',last_name AS 'Patient Name',doctor_name AS 'Patient Dr. Name',Discharge_Advice AS 'Dis Adv',Discharge_Card AS 'Dis Card',File_Check AS 'File Check',Billing,Actual_Discharge AS 'Actual Discharge',Time,reg_no,discharge_reason AS Remarks from NewDischargeTimes where dicharge_date=CONVERT(date, '" + maskedtxbx_dischargedate.Text + "', 103)", con);
SqlCommandBuilder cmd = new SqlCommandBuilder(da);
da.Fill(dtusers);
dataGridView1.DataSource = dtusers;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);



}
jeenamary 28-Sep-13 11:57am    
Where is dtusers declared?
alanmaster 30-Sep-13 4:40am    
dtusers declared globally

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Drawing.Printing;
using System.IO;

namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTable dtusers = new DataTable();
alanmaster 30-Sep-13 5:09am    
dtusers.Clear();


yes it worked thank u very much.

Clear your datagridview before you add rows with datagridview.rows.clear() or datagridview.datatasource = null and then the code to fill it up. Sorry i cant give the exact answer either one should work im answering from iphone and have no access to my notes
 
Share this answer
 
Hi,


If your datatable dtusers is declared outside the BTN_SHOW_Click, issue might be due to, the datatabe is persisting its old values.


Please add the line dtusers.Clear(); like as follows

C#
private void BTN_SHOW_Click(object sender, EventArgs e)
 {

dtusers.Clear();
SqlDataAdapter da = new SqlDataAdapter("select dicharge_date AS 'Discharge Date',bed_id AS 'Room No',level1 AS 'Floor No',last_name AS 'Patient Name',doctor_name AS 'Patient Dr. Name',Discharge_Advice AS 'Dis Adv',Discharge_Card AS 'Dis Card',File_Check AS 'File Check',Billing,Actual_Discharge AS 'Actual Discharge',Time,reg_no,discharge_reason AS Remarks from NewDischargeTimes where dicharge_date=CONVERT(date, '" + maskedtxbx_dischargedate.Text + "', 103)", con);
 SqlCommandBuilder cmd = new SqlCommandBuilder(da); 
da.Fill(dtusers); dataGridView1.DataSource = dtusers; 
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); 
}


Please let me if it worked!!
 
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