Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have Datagrid that I want to check the value it's getting from text box and validate if it's not duplicated records

Then if it's validate it continues posting api into my datagrid

Can you please help me what should I add?

my datagrid name is usergrid

What I have tried:

private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("https://localhost:44324/");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        var channel = new Channelindex();
        channel.name = txtName.Text;
        channel.url = txturl.Text;

        if (!string.IsNullOrWhiteSpace(txturl.Text) && !string.IsNullOrEmpty(txturl.Text))
        {
            if (txtName.Text == usergrid.ItemsSource.ToString())
            {
                MessageBox.Show("channel exists");
            }
            else
            {

                var response = client.PostAsJsonAsync("api/Channels", channel).Result;

                if (response.IsSuccessStatusCode)
                {
                    MessageBox.Show("Channel Added");
                    txtName.Text = "";
                    txturl.Text = "";
                    GetData();
                }
                else
                {
                    MessageBox.Show("Error Code" +
                  response.StatusCode + " : Message - " + response.ReasonPhrase);
                }

            }

        }


and my datagrid:

private void GetData()
  {
      HttpClient client = new HttpClient();
      client.BaseAddress = new Uri("https://localhost:44324/");
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

      HttpResponseMessage rsponse = client.GetAsync("api/Channels").Result;

      if (rsponse.IsSuccessStatusCode)
      {
          var channels = rsponse.Content.ReadAsAsync<IEnumerable<Channelindex>>().Result;
          usergrid.ItemsSource = channels;
      }
      else
      {
          MessageBox.Show("Error Code" +
      rsponse.StatusCode + " : Message - " + rsponse.ReasonPhrase);
      }
  }


and my ChannelIndex class :

namespace YoutubeIndex
{
    public class Channelindex
    {
        public int id { get; set; }
        public string name { get; set; }
        public string url { get; set; }
        public bool geeksIsForKids { get; set; }
        public bool geeksIsMusical { get; set; }


    }
        
}
Posted
Updated 4-Jan-22 23:15pm
v3
Comments
Richard MacCutchan 5-Jan-22 4:53am    
There is a lot of details, and a lot of code missing from your question.
Member 15489508 5-Jan-22 5:09am    
yes sorry.its edited

1 solution

As you have provided minimal details, I can only give a relatively high-level answer but the normal way to do this relies on you having used data binding to bind your grid to an underlying data source. What you would normally consider, in situations like this, is checking the input value to see that it is not present in the grid. Without any more information from you, I can't begin to show you how this would be accomplished.

You might want to consider adding details about the model that you are binding the datagrid to get a more detailed answer.

Okay, given your updates, you should be able to solve this with a couple of simple fixes. The first thing I would do is make channels a member variable rather than a local variable.
C#
private IEnumerable<ChannelIndex> channels;
private void GetData()
  {
      HttpClient client = new HttpClient();
      client.BaseAddress = new Uri("https://localhost:44324/");
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

      HttpResponseMessage rsponse = client.GetAsync("api/Channels").Result;

      if (rsponse.IsSuccessStatusCode)
      {
          channels = rsponse.Content.ReadAsAsync<IEnumerable<Channelindex>>().Result;
          usergrid.ItemsSource = channels;
      }
      else
      {
          MessageBox.Show("Error Code" +
      rsponse.StatusCode + " : Message - " + rsponse.ReasonPhrase);
      }
  }
With your populated enumerable, you now have the ability to find duplicates. To do this, you could use a simple LINQ query to check whether or not the item is present. Your duplicate check method might look something like this:
C#
private bool IsChannelAlreadyPresent(string url)
{
  return channels.Any((channel) => string.Equals(channel.url, url, StringComparison.CurrentCultureIgnoreCase));
}
You can then use this method from your add click handler to check for a duplicate entry.
 
Share this answer
 
v2
Comments
Member 15489508 5-Jan-22 5:09am    
yes sorry.its edited
Member 15489508 5-Jan-22 6:43am    
Thanks.It worked
Pete O'Hanlon 5-Jan-22 9:16am    
You're most welcome. I'm glad you're up and running.

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