winform设计中借助dataGridView实现窗体传值把值传到主窗体中,
报错显示未将对象引用设置到对象的实例
private void InformationSave_Load(object sender, EventArgs e)
{
//设置列的数目
dataGridView1.ColumnCount = 11;
dataGridView1.ColumnHeadersVisible = true;
//为每一个单元格添加标题
dataGridView1.Columns[0].Name = "图书编号";
dataGridView1.Columns[1].Name = "图书名称";
dataGridView1.Columns[2].Name = "入库时间";
dataGridView1.Columns[3].Name = "图书分类";
dataGridView1.Columns[4].Name = "作者";
dataGridView1.Columns[5].Name = "拼音码";
dataGridView1.Columns[6].Name = "页数";
dataGridView1.Columns[7].Name = "版本";
dataGridView1.Columns[8].Name = "存放位置";
dataGridView1.Columns[9].Name = "图书价格";
dataGridView1.Columns[10].Name = "内容描述";
BookAdd frm = (BookAdd)this.Owner;
try
{
frm.Controls["txtName"].Text = (string)dataGridView1.Rows[1].Cells[0].Value;
}
catch
{
MessageBox.Show("存放失败");
}

窗体运用程序中借助dataGridView接收传过来的值是显示未将对象引用设置到对象的实例
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- displaycool 2019-05-27 18:10关注
我发一段我的使用OK的程序给你参考,我定义了一个dataGridView,然后读取文件中一段内容并显示,可以参考
private bool Read_IO_Setting(string[] lines, ref string result) // string[] lines 是文件中读取的一段内容
{
if (lines == null || lines.Length == 0)
{
result = "I/O设定,内容为空";
return false;
}
int count = lines.Length;
List listStrArr = new List();//数组List,相当于可以无限扩大的二维数组。
for (int i = 0; i < count; i++)
{
string Line = lines[i];
listStrArr.Add(Line.Split(','));//将文件内容分割成数组
}dgvConfig.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; dgvConfig.CausesValidation = false; dgvConfig.RowCount = count; dgvConfig.ColumnCount = 5; for (int i = 0; i < dgvConfig.RowCount; i++) //RowCount { dgvConfig.Rows[i].Cells[0].Value = i+1; dgvConfig.Rows[i].Cells[1].Value = listStrArr[i][1]; dgvConfig.Rows[i].Cells[2].Value = listStrArr[i][2]; if (listStrArr[i][3].toInt() < 0 || listStrArr[i][3].toInt() > 63) { MessageBox.Show("I/O设定页-编号" + (i+1) + ":输出回路超出范围(0~63)" + "\n请修改并保存!"); } dgvConfig.Rows[i].Cells[3].Value = listStrArr[i][3]; dgvConfig.Rows[i].Cells[4].Value = listStrArr[i][4]; } return true; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报