Tuesday, 14 February 2017

DataGridview Duplicate Data Remove in C#

        public void RemoveDuplicate(DataGridView grv)
        {
            for (int currentRow = 0; currentRow < grv.Rows.Count - 1; currentRow++)
            {
                DataGridViewRow rowToCompare = grv.Rows[currentRow];

                for (int otherRow = currentRow + 1; otherRow < grv.Rows.Count; otherRow++)
                {
                    DataGridViewRow row = grv.Rows[otherRow];

                    bool duplicateRow = true;

                    for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)
                    {
                        if (!rowToCompare.Cells[cellIndex].Value.Equals(row.Cells[cellIndex].Value))
                        {
                            duplicateRow = false;
                            break;
                        }
                    }

                    if (duplicateRow)
                    {
                        grv.Rows.Remove(row);
                        otherRow--;
                    }
                }
          }
}
           
         

Sunday, 5 February 2017

Convert DataGridview to DataTable with BindingSource By JSon


     Model.Sell sell = new Model.Sell();
     BindingSource bindingSource = new BindingSource();

        private void btnAdd_Click(object sender, EventArgs e)
        {
                sell.ID=1;
               sell.Name=tbxName.Text;
               bindingSource.Add(sell);
               BindData();

        }

       private void BindData()
       {
            DataGridView1.DataSource=bindingSource;
        }


        private DataTable ConvertToDataTable()
        {
                DataTable dt = new DataTable();
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(bindingSource.List);
                dt = JsonConvert.DeserializeObject<DataTable>(json);          
               return dt;
        }

Comments system

Advertising

Disqus Shortname