0

The Display_Form_Load() Function is working fine.

private void Display_Form_Load(object sender, EventArgs e)
{
  try
    {
        using (SqlConnection cn = new 
        SqlConnection("Server=ZIKO_RED2486;Database=Students;Integrated Security=true"))
        {
            if (cn.State == ConnectionState.Closed)
                cn.Open();
            using (SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Student", cn))
            {
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

Id_Student is the name of the column in The Table.
I want to search by id so txt_Search_Id is the input:

private void Search_Click(object sender, EventArgs e)
{
    DataView DV = dt.DefaultView;
    DV.RowFilter = string.Format("CONVERT(Id_Student, System.String) like '%{0}%'", 
    txt_Search_Id);
    dataGridView1.DataSource = DV.ToTable();
}

When I click this Button, the search should be done, instead all the rows are removed from the DGV.
I was expecting to see the Rows filtered by the txt_Search_Id value.

I wish someone correct what i did wrong.

Jimi
  • 29,621
  • 8
  • 43
  • 61
Dzeko
  • 27
  • 6
  • 2
    What Type is `Id_Student`? Why are you trying to convert it to string? Also, you don't need to convert the DataTable.DefaultView to Table again, just `(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = $"Id_Student = {txt_Search_Id.Text}"`, assuming `txt_Search_Id` is a TextBox control and `Id_Student` is an numeric value. Otherwise, specify its Type. – Jimi Nov 29 '20 at 00:14
  • 1
    now it works fine. thanks sir. – Dzeko Nov 29 '20 at 00:25
  • check here i hope it's help :)) [click here ](https://stackoverflow.com/questions/13173915/search-for-value-in-datagridview-in-a-column) – zakaria daoudi Nov 29 '20 at 16:02

0 Answers0