我的代码无法从 form2 a 的 datagridview 添加项目到 form 1 的列表框。但它适用于插入到文本框
my code to add items to a list box of form1 from a datagrid view from form2 a isnt working. but it works for inserting to a textbox
表格 2
public string[] thislistboxitems;
private string pVal;
public string PassVal
{
get { return pVal; }
set { pVal = value; }
}
private void Form3_Load(object sender, EventArgs e)
{
ListBox1.Text = pVal;
}
表格 1
Form2 f = new Form2();
{
int selectedCellCount = dataGridView1.GetCellCount(DataGridViewElementStates.Selected);
if (selectedCellCount > 0)
{
for (int i = 0; i < selectedCellCount; i++)
{
int column = dataGridView1.SelectedCells[i].ColumnIndex;
int row = dataGridView1.SelectedCells[i].RowIndex;
f.PassVal = dataGridView1[column, row].Value.ToString();
}
}
f.Show();
}
您需要将所选值添加为列表框中的项目。
private void Form3_Load(object sender, EventArgs e)
{
ListBox1.Items.Add(pVal);
}
在 Form2 中创建一个可以从 Form1 调用的 public 方法
public void AddToListBox(string itemText)
{
this.ListBox1.Items.Add(itemText);
}
并在 Form1 中更改循环以调用此方法
Form2 f = new Form2();
int selectedCellCount = dataGridView1.GetCellCount(DataGridViewElementStates.Selected);
if (selectedCellCount > 0)
{
for (int i = 0; i < selectedCellCount; i++)
{
int column = dataGridView1.SelectedCells[i].ColumnIndex;
int row = dataGridView1.SelectedCells[i].RowIndex;
f.AddToListBox(dataGridView1[column, row].Value.ToString());
}
}
f,Show();
表格 2
public string[] thislistboxitems;
private string pVal;
public string PassVal
{
get { return pVal; }
set { pVal = value; }
}
private void Form3_Load(object sender, EventArgs e)
{
ListBox1.Text = pVal;
}
表格 1
Form2 f = new Form2();
{
int selectedCellCount = dataGridView1.GetCellCount(DataGridViewElementStates.Selected);
if (selectedCellCount > 0)
{
for (int i = 0; i < selectedCellCount; i++)
{
int column = dataGridView1.SelectedCells[i].ColumnIndex;
int row = dataGridView1.SelectedCells[i].RowIndex;
f.PassVal = dataGridView1[column, row].Value.ToString();
}
}
f.Show();
}
您需要将所选值添加为列表框中的项目。
private void Form3_Load(object sender, EventArgs e)
{
ListBox1.Items.Add(pVal);
}
在 Form2 中创建一个可以从 Form1 调用的 public 方法
public void AddToListBox(string itemText)
{
this.ListBox1.Items.Add(itemText);
}
并在 Form1 中更改循环以调用此方法
Form2 f = new Form2();
int selectedCellCount = dataGridView1.GetCellCount(DataGridViewElementStates.Selected);
if (selectedCellCount > 0)
{
for (int i = 0; i < selectedCellCount; i++)
{
int column = dataGridView1.SelectedCells[i].ColumnIndex;
int row = dataGridView1.SelectedCells[i].RowIndex;
f.AddToListBox(dataGridView1[column, row].Value.ToString());
}
}
f,Show();