使用组合框来获取要在DataGridView中显示的数据以过滤查询结果
我的组无法让此组合框和datagridview相互交谈。应该发生的事情是,当你从组合框中选择一个名字时,任何带有技术ID的开放事件都应该出现。我们已经有了过滤器的工作,但我们似乎无法让两者相互交谈。这里是我们迄今的代码:使用组合框来获取要在DataGridView中显示的数据以过滤查询结果
public partial class frmIncidentMaintenance : Form
{
public Incident incident;
public frmIncidentMaintenance()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TechSupportEntities techSupport = new TechSupportEntities();
var customers = (from customer in techSupport.Customers
orderby customer.Name
select new { customer.CustomerID, customer.Name
}).Distinct();
cmbCustomersBindingSource.DataSource = customers.ToList();
cmbCustomersBindingSource.DisplayMember = "Name";
cmbCustomersBindingSource.ValueMember = "CustomerID";
var products = from customer in techSupport.Customers
from incident in customer.Incidents
where incident.TechID != null
where incident.DateClosed == null
select new
{
incident.ProductCode,
incident.TechID,
incident.Title,
incident.DateOpened,
incident.DateClosed,
incident.Description
};
dataGridView1.DataSource = products.ToList();
}
private void cmbCustomers_SelectedIndexChanged(object sender, EventArgs
e)
{
}
private void dataGridView_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
任何帮助,将不胜感激。
private void cmbCustomers_SelectedIndexChanged(object sender, EventArgs
e)
{
//This is the string for your tech id
string tech_id = combobox.SelectedItem.ToString();
//Searches the datagridview
int rowIndex =0;
foreach(DataGridViewRow row in [name of grid here])
{
//matches tech-id to gridrow value
if(tech_id == row.Cells[//Cell for tech_id].Value.ToString())
{
//Looks for open incidents (ill guess a numberical value 1 or 0
int open_close_value = Convert.ToInt16(row.Cells[openvalue]contains this data].value.ToString());
if(open_close_value == value your looking for)
{
//rowindex will give you the row of the gridview
rowindex = row.Index;
string incident = row.Cells[you incident].Value.ToString();
// for pushing data to be displayed in textboxes make it
// easier on yourself and allow the search engine to pull
//all incidents that are open with that tech_id add them to a list
//this will add the row number plus the incident
Listbox1.Items.Add(rowindex+","+incident);
//now on listbox1 selectedindex change all you do is parse rowindex, and use that rownumber to pull data out of database.
// your parse string should look like this.
//string[] data = listbox1.Selectedindex.ToString().Split(new string[] {","}, StringSplitOptions.None);
//to get data from gridview
//string incident= yourGrid.Rows[data[0]].Cell[the cell you want].value.ToString();
}
这将拉开所有已知开放事件的技术ID,并允许您从列表中选择一个。显示。希望它能帮助你。 –
有没有办法在DataGridView中显示相同的信息,迈克尔?我们希望能够在某些栏目中更改信息。 – Venomsamurai
声明要添加到网格中的变量。然后使用GridName.Rows.Add(// variable1,variable2);按照网格的列顺序放置您的变量。意思是,如果产品在列0中,那么当添加一行时,它是第一个变量。第1列将是第二列。等等。 –
private void cmbCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
//This is the string for your tech id
string tech_id = combobox.SelectedItem.ToString();
//Searches the datagridview
foreach(DataGridViewRow row in [name of grid here])
{
//matches tech-id to gridrow value
if(tech_id == row.Cells[//Cell for tech_id].Value.ToString())
{
//Looks for open incidents (ill guess a numberical value 1 or 0
int open_close_value = Convert.ToInt16(row.Cells[openvalue]contains this data].value.ToString());
if(open_close_value == value your looking for)
{
//rowindex will give you the row of the gridview
rowindex = row.Index;
string incident = row.Cells[you incident].Value.ToString();
// for pushing data to be displayed in textboxes make it
// easier on yourself and allow the search engine to pull
//all incidents that are open with that tech_id add them to a grid
//this will add the row number plus the incident
NewGrid.Rows.Add(row.Cells[0].Value, row.Cells[1].Value,row.Cells[2].Value, row.Cells[3].Value,row.Cells[4].Value,row.Cells[5].value);
//now on listbox1 selectedindex change all you do is parse rowindex, and use that rownumber to pull data out of database.
// your parse string should look like this.
//string[] data = listbox1.Selectedindex.ToString().Split(new string[] {","}, StringSplitOptions.None);
//to get data from gridview
//string incident= yourGrid.Rows[data[0]].Cell[the cell you want].value.ToString();
}
这将添加到一个单独的gridview –
你必须把一些逻辑在cmbCustomers_SelectedIndexChanged,例如,使接收的参数(cmbCustomers的SelectedValue)的方法,然后调用它每次cmbCustomers触发事件的时间。 – JCM
像什么JCM?我和我的两个合作伙伴已经为此工作了两周,并且我们一直在SelectedIndexChanged事件处理程序中停滞不前。 – Venomsamurai