I have bounded data to datagridview
from DataSet
and I am trying to filter
these bounded data within datagridview
on event of textchange
I got two issues when
I start typing it work fine except it delete the custom
datagridview
headers and set the name of columns query ex. Header is 'First Name' it replaced by 'NAM' which is the column name at database...Second issue when I came into else part it wont re-bounded and throw an Exception what I have missed ?
public DataSet GetPatientList() { string connStr = ConfigurationManager.ConnectionStrings["SRJDconnstr"].ToString(); string cmdStr = @"SELECT ROW_NUMBER()OVER(ORDER BY ID) AS SEQ, ID, DocNUM, NAM, FNAME, LFNAME, PHONE, MOBILE, SEX, BIRTHDAY, ADDRESS, ENDATETIME FROM SICK ORDER BY ENDATETIME ASC;"; SqlConnection conn = new SqlConnection(connStr); using (SqlCommand cmd = new SqlCommand(cmdStr, conn)) { conn.Open(); cmd.CommandText = cmdStr; cmd.CommandType = CommandType.Text; ds = new DataSet(); da = new SqlDataAdapter(cmd); da.Fill(ds, "PatientList"); DGV_PatientList.Columns["DGV_PatientList_RowNum"].DataPropertyName = ds.Tables["PatientList"].Columns["SEQ"].ColumnName; DGV_PatientList.Columns["DGV_PatientList_PatientID"].DataPropertyName = ds.Tables["PatientList"].Columns["ID"].ColumnName; DGV_PatientList.Columns["DGV_PatientList_DocNUM"].DataPropertyName = ds.Tables["PatientList"].Columns["DocNUM"].ColumnName; DGV_PatientList.Columns["DGV_PatientList_FirstName"].DataPropertyName = ds.Tables["PatientList"].Columns["NAM"].ColumnName; DGV_PatientList.Columns["DGV_PatientList_FatherName"].DataPropertyName = ds.Tables["PatientList"].Columns["FNAME"].ColumnName; DGV_PatientList.Columns["DGV_PatientList_LastName"].DataPropertyName = ds.Tables["PatientList"].Columns["LFNAME"].ColumnName; DGV_PatientList.Columns["DGV_PatientList_Phone"].DataPropertyName = ds.Tables["PatientList"].Columns["PHONE"].ColumnName; DGV_PatientList.Columns["DGV_PatientList_Mobile"].DataPropertyName = ds.Tables["PatientList"].Columns["MOBILE"].ColumnName; DGV_PatientList.Columns["DGV_PatientList_Gender"].DataPropertyName = ds.Tables["PatientList"].Columns["SEX"].ColumnName; DGV_PatientList.Columns["DGV_PatientList_Birthday"].DataPropertyName = ds.Tables["PatientList"].Columns["BIRTHDAY"].ColumnName; DGV_PatientList.Columns["DGV_PatientList_Address"].DataPropertyName = ds.Tables["PatientList"].Columns["ADDRESS"].ColumnName; DGV_PatientList.Columns["DGV_PatientList_EntryDate"].DataPropertyName = ds.Tables["PatientList"].Columns["ENDATETIME"].ColumnName; return ds; } }
Text Change event
private void TB_FirstName_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(TB_FirstName.Text))
{
// first try below
(ds.Tables["PatientList"] as DataTable).DefaultView.RowFilter = string.Format("NAM LIKE '%{0}%'", TB_FirstName.Text);
// second try below
//ds.Tables["PatientList"].DefaultView.RowFilter = string.Format("NAM LIKE '%{0}%'", TB_FirstName.Text);
}
else
{
DGV_PatientList.DataSource = GetPatientList();
DGV_PatientList.DataSource = ds.Tables["PatientList"].DefaultView;
}
}