如何在网格中保存数据 [英] How to save data in a Grid
本文介绍了如何在网格中保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void addbutton_Click(object sender, EventArgs e)
{
int StuID = Convert.ToInt32(this.SIDtextBox.Text);
DataTable dt = cslStudent.GetStudents(StuID);
this.SelectedStudentGridView.DataSource = dt;
BindingSource bs = new BindingSource();
bs.DataSource = dt;
int iRowCnt = this.SelectedContainersdataGridView.Rows.Count;
string StudName = this. SelectedStudentGridView.Rows[iRowCnt].Cells[0].Value.ToString();
string Address = this. SelectedStudentGridView.Rows[iRowCnt].Cells[1].Value.ToString();
string Phone = this. SelectedStudentGridView.Rows[iRowCnt].Cells[2].Value.ToString();
PHTS.DataServices.cslStudent.NewStudent(StuID, StudName, Address, Phone);
MessageBox.Show("Student Added Sucessfully");
}
我在将数据保存到数据库时遇到麻烦.我收到索引超出范围.必须为非负数并且小于集合的大小."
谢谢
I am having trouble in saving data to the database. I am getting "Index was out of range. Must be non-negative and less than the size of the collection."
Thanks
推荐答案
在访问Cells
的Row
之前,需要先创建它.当然,这是使用自己的变量名的一种方法
Before you can access aRow
ofCells
, you need to create it. Here''s one way to do it, using your own variable names, of course
int n = grid.Rows.Add();
DataGridViewRow r = grid.Rows[n];
r.Cells[0].Value = ... ;
这篇关于如何在网格中保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文