Hi @ankit goel , Welcome to Microsoft Q&A.
Here is the custom datagridview: Which creates a mock list of the database (do the replacements you need).
Change the data source of suggestdata2 to list2.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace_7_3_cc1
{
public partial class CustomDataview2: DataGridView
{
public DataGridView suggestivedatagridview;
public class Product
{
public string Name
{
get;
set;
}
public decimal Price
{
get;
set;
}
}
// Define a List<Product> as a data source in the form class
public List < Product > products = new List < Product > ();
private List < Product > products2 = new List < Product > ();
//
int rowNowNumber = -1;
int rowMaxNuber = -1;
public CustomDataview2()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
protected override void OnCellValueChanged(DataGridViewCellEventArgs e)
{
base.OnCellValueChanged(e);
if(this.SelectedCells.Count > 0)
{
DataGridViewCell dataGridCell = this.SelectedCells[0];
if(dataGridCell.ColumnIndex == 0 && dataGridCell.RowIndex >= 0) // Assume the string to be filtered is in the first column
{
if(dataGridCell.Value != null)
{
suggestivedatagridview.Visible = true;
string filterString = this.Rows[dataGridCell.RowIndex].Cells[dataGridCell.ColumnIndex].Value.ToString();
// Clear the previous filter results
products2.Clear();
// Filter the products list and add qualified data to the products2 list
products2 = products.Where(p => p.Name.Contains(filterString)).ToList();
// Bind the new filter result to dataGridView2
suggestivedatagridview.DataSource = products2;
suggestivedatagridview.CurrentCell = null;
if(products2 != null)
{
suggestivedatagridview.Rows[0].Selected = true;
rowNowNumber = 0;
rowMaxNuber = suggestivedatagridview.Rows.Count;
}
CurrentCell = dataGridCell;
BeginEdit(true);
}
}
}
}
protected override void OnEnter(EventArgs e)
{
// Set the current cell to the first cell and enable editing when the control receives focus
if(RowCount > 0 && ColumnCount > 0)
{
CurrentCell = Rows[0].Cells[0];
BeginEdit(true);
}
base.OnEnter(e);
}
protected override bool ProcessDialogKey(Keys keyData)
{
DataGridViewCell dataGridCell = this.SelectedCells[0];
if(keyData == Keys.Enter)
{
this.CommitEdit(DataGridViewDataErrorContexts.Commit);
return true;
}
return base.ProcessDialogKey(keyData);
}
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
DataGridViewCell dataGridCell = this.SelectedCells[0];
if(IsCurrentCellInEditMode)
{
if(e.KeyCode == Keys.Up)
{
// CurrentCell = dataGridCell;
if(!suggestivedatagridview.Rows[0].IsNewRow)
{
if((rowNowNumber - 1) >= 0)
{
suggestivedatagridview.Rows[rowNowNumber].Selected = false;
suggestivedatagridview.Rows[--rowNowNumber].Selected = true;
}
}
BeginEdit(true);
return true;
}
else if(e.KeyCode == Keys.Down)
{
//CurrentCell = null;
// CurrentCell = dataGridCell;
if(!suggestivedatagridview.Rows[0].IsNewRow)
{
if((rowNowNumber + 1) < rowMaxNumber)
{
suggestivedatagridview.Rows[rowNowNumber].Selected = false;
suggestivedatagridview.Rows[++rowNowNumber].Selected = true;
}
}
BeginEdit(true);
return true;
}
}
return base.ProcessDataGridViewKey(e);
}
}
}
Form1: After dragging into the custom datagridview, you need to pass in suggestdata2 and data in the code page. Add the required column by yourself.
using System.Collections.Generic;
using System.Windows.Forms;
namespace_7_6_1
{
public partial class Form1: Form
{
// Define a List<Product> as a data source in the form class
private List < _7_3_cc1.CustomDataview2.Product > products = new List < _7_3_cc1.CustomDataview2.Product > ();
public Form1()
{
InitializeComponent();
// add some sample data
products.Add(new _7_3_cc1.CustomDataview2.Product
{
Name = "ranjan", Price = 10.99 m
});
products.Add(new _7_3_cc1.CustomDataview2.Product
{
Name = "abhigyan", Price = 20.50 m
});
products.Add(new _7_3_cc1.CustomDataview2.Product
{
Name = "tunjan", Price = 15.75 m
});
products.Add(new _7_3_cc1.CustomDataview2.Product
{
Name = "xxxxx", Price = 20.75 m
});
// hide d2
suggestivedatagridview.Visible = false;
customDataview21.products = products;
customDataview21.suggestivedatagridview = suggestivedatagridview;
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.