none
Agenda of Contacts in Visual Studio RRS feed

  • Pregunta

  • Hello,

    I have to make an agenda of contacts which will contain the name,surname,phone number and the date of birth of someone. I have saved the data in dataGridView. May you tell me how to do search in these data based on name,surname and phone number?

    Thank you in advance.

    viernes, 12 de abril de 2019 14:21

Respuestas

  • Hi AndriannaTs,

    As an example, I make a query by name. The specific code is shown as follows:

        private void btSearch_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dr in dataGridView1.Rows)
            {
                if (dr.Cells[0].Value != null && dr.Cells[0].Value.ToString() == tbName.Text)
                {
                    DateTime dateTime = (DateTime)dr.Cells[3].Value;
                    MessageBox.Show("surname: " + dr.Cells[1].Value.ToString() + "\nphone: " + dr.Cells[2].Value.ToString() + "\nbirth: " + dateTime.ToShortDateString());
                    return;
                }
            }
        }

    Result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marcado como respuesta AndriannaTs martes, 7 de mayo de 2019 22:28
    lunes, 15 de abril de 2019 6:00
    Moderador

Todas las respuestas

  • Hi AndriannaTs,

    Welcome to the MSDN forum.

    According to your description, this issue is about the Windows Forms development and our forum is to discuss the VS IDE, I will help you move this thread to the appropriate forum for better support, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    lunes, 15 de abril de 2019 2:18
  • Hi AndriannaTs,

    As an example, I make a query by name. The specific code is shown as follows:

        private void btSearch_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dr in dataGridView1.Rows)
            {
                if (dr.Cells[0].Value != null && dr.Cells[0].Value.ToString() == tbName.Text)
                {
                    DateTime dateTime = (DateTime)dr.Cells[3].Value;
                    MessageBox.Show("surname: " + dr.Cells[1].Value.ToString() + "\nphone: " + dr.Cells[2].Value.ToString() + "\nbirth: " + dateTime.ToShortDateString());
                    return;
                }
            }
        }

    Result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marcado como respuesta AndriannaTs martes, 7 de mayo de 2019 22:28
    lunes, 15 de abril de 2019 6:00
    Moderador
  • Hello,

    An easy way to achieve this is to use a DataTable to store your information, creating a BindingSource component at form level. Set the DataTable to become the .DataSource of the BindingSource then use the following component (takes one line of code) to get the following.

    Simply click on the column(s) you want to filter set the condition e.g. equals, starts with, contains or ends with following by the data to search for. You can remove one or all filters via X buttons in each column or one X for removal of all filters.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    lunes, 15 de abril de 2019 12:39
    Moderador
  • Hi AndriannaTs,

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer.So that it will help other members to find the solution quickly if they face the similar issue.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    jueves, 18 de abril de 2019 6:36
    Moderador
  • I have one more question. I have to save my data from DataGridView in a txt file. Is it better to send them in a listBox first or to save them directly from DataGridView? I tried to send them in a listBox but there is not empty space between the context of textboxes. For example,I have the name: Andrianna,surname:Ts,phonenumber:12345678 and date of birth:15/07/2020 and the result in the txt file is AndriannaTs1234567815/07/2020 instead of  Andrianna Ts 12345678 15/07/2020. I tried also to put " " but it doesn't work.  How can I solve it?

    This is the code that I used:

    private void button4_Click(object sender, EventArgs e)
            {
                SaveFileDialog dlg = new SaveFileDialog();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    StreamWriter writer = new StreamWriter(dlg.FileName);

                    for (int i = 0; i < listBox1.Items.Count; i++)
                    {
                        writer.WriteLine((string)listBox1.Items[i]);
                    }
                    writer.Close();
                }
                dlg.Dispose();
            }

    Thank you in advance.

    martes, 7 de mayo de 2019 23:40
  • Hi AndriannaTS,

    One thread only discusses one issue. If you have any other new issue, please create another thread and post the issue on it.

    Thank you for your understanding.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    miércoles, 8 de mayo de 2019 5:56
    Moderador