大家帮忙看一下这个代码有啥问题啊,反正是结果不正确,谢谢。只想着实现功能,水平又差,结果写出来的东西转晕了我。功能大致就是从数据库中取出值,动态赋值给控件,然后将控件改变后的值存入数据库public int cNum;
public string[] lbl;
//显示控件
private void show1()
{ string cStr = string.Format("select A1 from A1 where B1='{0}'", label4.Text.Trim()); SqlDataAdapter cSDA = new SqlDataAdapter(cStr, DB.cn);
DataSet cDS = new DataSet();
cSDA.Fill(cDS, "A1");
DataTable cDT = new DataTable();
cDT = cDS.Tables[0].Copy();
cNum = cDT.Rows.Count; int i = 0;
lbl = new string[cNum];
foreach (DataRow row in cDT.Rows)
{
//添加Label
Label label = new Label();
label.Text = row["A1"].ToString();
lbl[i] = row["A1"].ToString();
label.Name = "label_" + i;
label.Size = new Size(110, 30);
label.Location = new Point(180, 30 * (i + 1));
Controls.Add(label); //添加textbox
TextBox textBox = new TextBox();
textBox.Text = "";
textBox.Name = "textboxCQ_" + i;
textBox.Size = new Size(120, 30);
textBox.Location = new Point(290, 30 * (i + 1));
Controls.Add(textBox); i++; }
}//存入数据库
private void button1_Click(object sender, EventArgs e)
{
foreach(Control c in Controls)
{
if (c is TextBox && ((TextBox)c).Name.Contains("textboxCQ_"))
{
string conCqStr = "insert into [B1]([B1],[B2]) values('" + ((TextBox)c).Text + "','"+lbl[i]+"')";
SqlCommand addCqCmd = new SqlCommand(conCqStr, DB.cn); DB.cn.Open();
addCqCmd.ExecuteNonQuery();
DB.cn.Close();
i++;
}
}
}显示没问题,存入数据库时,label只能存最后一个,textbox则完全不正确
public string[] lbl;
//显示控件
private void show1()
{ string cStr = string.Format("select A1 from A1 where B1='{0}'", label4.Text.Trim()); SqlDataAdapter cSDA = new SqlDataAdapter(cStr, DB.cn);
DataSet cDS = new DataSet();
cSDA.Fill(cDS, "A1");
DataTable cDT = new DataTable();
cDT = cDS.Tables[0].Copy();
cNum = cDT.Rows.Count; int i = 0;
lbl = new string[cNum];
foreach (DataRow row in cDT.Rows)
{
//添加Label
Label label = new Label();
label.Text = row["A1"].ToString();
lbl[i] = row["A1"].ToString();
label.Name = "label_" + i;
label.Size = new Size(110, 30);
label.Location = new Point(180, 30 * (i + 1));
Controls.Add(label); //添加textbox
TextBox textBox = new TextBox();
textBox.Text = "";
textBox.Name = "textboxCQ_" + i;
textBox.Size = new Size(120, 30);
textBox.Location = new Point(290, 30 * (i + 1));
Controls.Add(textBox); i++; }
}//存入数据库
private void button1_Click(object sender, EventArgs e)
{
foreach(Control c in Controls)
{
if (c is TextBox && ((TextBox)c).Name.Contains("textboxCQ_"))
{
string conCqStr = "insert into [B1]([B1],[B2]) values('" + ((TextBox)c).Text + "','"+lbl[i]+"')";
SqlCommand addCqCmd = new SqlCommand(conCqStr, DB.cn); DB.cn.Open();
addCqCmd.ExecuteNonQuery();
DB.cn.Close();
i++;
}
}
}显示没问题,存入数据库时,label只能存最后一个,textbox则完全不正确
解决方案 »
- java用httpClient post方式传数据,ASP.NET怎么接收数据?
- wince中使用httpwebrequest如何保存session
- winform中调用自定义控件的事件比如鼠标单击事件等 急急
- ****求高手分享,问一个小问题****
- 。net 有没有float[*,*] 这类型的?
- MDI子窗体和主窗体的问题,在线急等啊!!!!!!!!!!!!!!!!!!!1
- 急啊!Winform程序请问如何去掉TextBox.Text中那些回车和换行符
- 我有个棘手的问题,大家来参考参考
- 排列图片的问题!
- 关于C#将数据导入excel文件后保存的问题
- 如何通过程序模拟CPU的使用率
- c#程序封装之后
{
DataTable dtSource = (从数据库取得数据);
//将DataTable的更改提交
dtSource.AcceptChange();//将数据源的值绑定到控件textBox1的Text属性上面去,如果是ComboBox之类的控件,可以绑定到SelectedValue属性...//这个是数据绑定到控件的关键
textBox1.DataBindings.Add("Text", dtSource,"dtSource对应该文本框的列名字", true, DataSourceUpdateMode.OnPropertyChanged);
}
//这个时候你手工修改textBox1中的文本值//保存到数据表
private void Save()
{
//这里可以检测到textBox1绑定后的dtSource相应列中值已经变成了你刚才手工更改过的值,而且这个时候这行数据的RowState已经变成了Modified状态
//然后调用DataAdpter将dtSource的DataSet保存到数据库中}