首页上一页 1 下一页尾页 3 条记录 1/1页
关于C#入门到精通中例15.14的问题
发表在C#图书答疑
2010-10-23
是否精华
是
否
版块置顶:
是
否
老师你好,我自已建个表,代码都仿书中例15.14写,调试通过,操作显示“索引超出范围,必需国非负值合小于集合的大小 参数名index”什么意思。我的表是
编号 int(4 )
姓名 varchar(16)
年龄 int(4)
性名 char(2)
且我没用到什么index参数
代码为
private void Form1_Load(object sender, EventArgs e)
{
string strconn = "server=.;database=DB_stu;integrated security=true";
conn = new SqlConnection(strconn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
string strcmd = "select * from tb_stu";
cmd.CommandText = strcmd;
cmd.CommandType = CommandType.Text;
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
ds = new DataSet();
sda.Fill(ds,"dd");
dataGridView1.DataSource=ds.Tables[0];
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables["dd"];
sda.FillSchema(dt,SchemaType.Mapped);
DataRow dr = dt.Rows.Find(txtID.Text);
dr["姓名"] = txtName.Text.Trim();
dr["性别"] = txtSex.Text.Trim();
dr["年龄"]=txtAge.Text.Trim();
SqlCommandBuilder sddd = new SqlCommandBuilder(sda);
sda.Update(dt);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
txtID.Text = dataGridView1.SelectedCells[0].Value.ToString();
txtName.Text = dataGridView1.SelectedCells[1].Value.ToString();
txtSex.Text = dataGridView1.SelectedCells[2].Value.ToString();
txtAge.Text = dataGridView1.SelectedCells[3].Value.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
编号 int(4 )
姓名 varchar(16)
年龄 int(4)
性名 char(2)
且我没用到什么index参数
代码为
private void Form1_Load(object sender, EventArgs e)
{
string strconn = "server=.;database=DB_stu;integrated security=true";
conn = new SqlConnection(strconn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
string strcmd = "select * from tb_stu";
cmd.CommandText = strcmd;
cmd.CommandType = CommandType.Text;
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
ds = new DataSet();
sda.Fill(ds,"dd");
dataGridView1.DataSource=ds.Tables[0];
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables["dd"];
sda.FillSchema(dt,SchemaType.Mapped);
DataRow dr = dt.Rows.Find(txtID.Text);
dr["姓名"] = txtName.Text.Trim();
dr["性别"] = txtSex.Text.Trim();
dr["年龄"]=txtAge.Text.Trim();
SqlCommandBuilder sddd = new SqlCommandBuilder(sda);
sda.Update(dt);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
txtID.Text = dataGridView1.SelectedCells[0].Value.ToString();
txtName.Text = dataGridView1.SelectedCells[1].Value.ToString();
txtSex.Text = dataGridView1.SelectedCells[2].Value.ToString();
txtAge.Text = dataGridView1.SelectedCells[3].Value.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}