【发布时间】:2021-11-01 09:23:06
【问题描述】:
我有 2 个 Windows 窗体。在第一个表单上,我有一个 DataGridView,在第二个表单上,我只有几个 checkListBoxes。
我发现这个问题类似于我的:Filtering datagridview from another form
它看起来与我的问题相似,但它并没有说明我如何从第二种形式中过滤信息。
另外,我已经问过一些关于这个项目的问题:How can I load in Windows Forms? (C#)
这有助于更好地理解这种情况。
我的想法是你必须在第二个表单上做一些标记,它现在正在保存你点击的内容,但我需要在 DGV 的第一个表单上获得这些点击。
问题是:如何做一个有两种形式的过滤器?
代码:
private void button2_Click_2(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.InitializeSecondForm();
form2.Show();
}
public void InitializeSecondForm()
{
this.Height = Properties.Settings.Default.SecondFormHeight;
this.Width = Properties.Settings.Default.SecondFormWidth;
this.Location = Properties.Settings.Default.SecondFormLocation;
this.collectionOfTags = Properties.Settings.Default.DICOMTagSettings;
this.FormClosing += SecondFormClosingEventHandler;
this.StartPosition = FormStartPosition.Manual;
}
private void SecondFormClosingEventHandler(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.SecondFormHeight = this.Height;
Properties.Settings.Default.SecondFormWidth = this.Width;
Properties.Settings.Default.SecondFormLocation = this.Location;
Properties.Settings.Default.DICOMTagSettings = this.collectionOfTags;
Properties.Settings.Default.Save();
}
private void fileListDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DicomFileInfo dicomFile = new DicomFileInfo();
DataGridView datas = new DataGridView();
datas.DataSource = dicomFile;
for(int i = 0; i < datas.Rows.Count; i++)
{
DataGridViewLinkCell linkCell = new DataGridViewLinkCell();
datas[2, i] = linkCell;
}
}
第二种形式的代码:
public partial class Form2 : Form
{
StringCollection collectionOfTags = new StringCollection();
public Form2()
{
InitializeComponent();
InitializeSecondForm();
LoadSettings();
}
private void LoadSettings()
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
var d = checkedListBox1.Items[i];
if (collectionOfTags.Contains(d.ToString()))
{
int index = checkedListBox1.Items.IndexOf(d);
if (index != -1)
checkedListBox1.SetItemChecked(index, true);
}
}
for (int i = 0; i < checkedListBox2.Items.Count; i++)
{
var d = checkedListBox2.Items[i];
if (collectionOfTags.Contains(d.ToString()))
{
int index = checkedListBox2.Items.IndexOf(d);
if (index != -1)
checkedListBox2.SetItemChecked(index, true);
}
}
for (int i = 0; i < checkedListBox3.Items.Count; i++)
{
var d = checkedListBox3.Items[i];
if (collectionOfTags.Contains(d.ToString()))
{
int index = checkedListBox3.Items.IndexOf(d);
if (index != -1)
checkedListBox3.SetItemChecked(index, true);
}
}
for (int i = 0; i < checkedListBox4.Items.Count; i++)
{
var d = checkedListBox4.Items[i];
if (collectionOfTags.Contains(d.ToString()))
{
int index = checkedListBox4.Items.IndexOf(d);
if (index != -1)
checkedListBox4.SetItemChecked(index, true);
}
}
for (int i = 0; i < checkedListBox5.Items.Count; i++)
{
var d = checkedListBox5.Items[i];
if (collectionOfTags.Contains(d.ToString()))
{
int index = checkedListBox5.Items.IndexOf(d);
if (index != -1)
checkedListBox5.SetItemChecked(index, true);
}
}
for (int i = 0; i < checkedListBox6.Items.Count; i++)
{
var d = checkedListBox6.Items[i];
if (collectionOfTags.Contains(d.ToString()))
{
int index = checkedListBox6.Items.IndexOf(d);
if (index != -1)
checkedListBox6.SetItemChecked(index, true);
}
}
}
public void InitializeSecondForm()
{
this.Height = Properties.Settings.Default.SecondFormHeight;
this.Width = Properties.Settings.Default.SecondFormWidth;
this.Location = Properties.Settings.Default.SecondFormLocation;
this.collectionOfTags = Properties.Settings.Default.DICOMTagSettings;
this.FormClosing += SecondFormClosingEventHandler;
this.StartPosition = FormStartPosition.Manual;
}
private void SecondFormClosingEventHandler(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.SecondFormHeight = this.Height;
Properties.Settings.Default.SecondFormWidth = this.Width;
Properties.Settings.Default.SecondFormLocation = this.Location;
Properties.Settings.Default.DICOMTagSettings = this.collectionOfTags;
Properties.Settings.Default.Save();
}
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.DICOMTagSettings.Clear();
Properties.Settings.Default.DICOMTagSettings
.AddRange(checkedListBox1.CheckedItems.Cast<string>().ToArray());
foreach (string s in Properties.Settings.Default.DICOMTagSettings)
{
int index = checkedListBox1.Items.IndexOf(s);
if (index != -1) checkedListBox1.SetItemChecked(index, true);
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
}
}
Properties.Settings.Default.DICOMTagSettings
.AddRange(checkedListBox2.CheckedItems.Cast<string>().ToArray());
foreach (string t in Properties.Settings.Default.DICOMTagSettings)
{
int index = checkedListBox2.Items.IndexOf(t);
if (index != -1) checkedListBox2.SetItemChecked(index, true);
}
Properties.Settings.Default.DICOMTagSettings
.AddRange(checkedListBox3.CheckedItems.Cast<string>().ToArray());
foreach (string c in Properties.Settings.Default.DICOMTagSettings)
{
int index = checkedListBox3.Items.IndexOf(c);
if (index != -1) checkedListBox3.SetItemChecked(index, true);
}
Properties.Settings.Default.DICOMTagSettings
.AddRange(checkedListBox4.CheckedItems.Cast<string>().ToArray());
foreach (string a in Properties.Settings.Default.DICOMTagSettings)
{
int index = checkedListBox4.Items.IndexOf(a);
if (index != -1) checkedListBox4.SetItemChecked(index, true);
}
Properties.Settings.Default.DICOMTagSettings
.AddRange(checkedListBox5.CheckedItems.Cast<string>().ToArray());
foreach (string k in Properties.Settings.Default.DICOMTagSettings)
{
int index = checkedListBox5.Items.IndexOf(k);
if (index != -1) checkedListBox5.SetItemChecked(index, true);
}
Properties.Settings.Default.DICOMTagSettings
.AddRange(checkedListBox6.CheckedItems.Cast<string>().ToArray());
foreach (string q in Properties.Settings.Default.DICOMTagSettings)
{
int index = checkedListBox6.Items.IndexOf(q);
if (index != -1) checkedListBox6.SetItemChecked(index, true);
}
this.Close();
}
【问题讨论】:
-
所以您有 2 个带有 2 个不同数据源的表单窗口和另一个您可以点击一些复选框来过滤显示在 2 个表单上的数据?
-
@FlorianSchmidinger 我只有 2 个表格。在第一个表单上,我可以单击文件按钮,然后将显示第二个表单。在第二个表单上,您可以过滤,您需要在 DataGridView 中的第一个表单上查看哪些项目。
-
我看到图片很有帮助
-
您希望结果网格在您在第二个表单中打勾时在第一个表单上动态更改,还是您会单击“确定”然后执行过滤?您期待什么样的程序流程?不同类别的过滤器将如何组合? “OR 之内,AND 之间?”
-
链接回答将为您指明方向。您需要在表单之间进行引用。
标签: c# winforms datagridview checkedlistbox