private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string myConnString = "workstation id=FREEWILLER;packet size=4096;user id=sa;data source=FREEWILLER;persist security info=True;initial catalog=Northwind;password=goodsf";
nwindConn = new SqlConnection(myConnString);
nwindConn.Open();
if(!Page.IsPostBack)
BindGrid();
} public ICollection CreateTable()
{
string strSelect = "Select Top 10 * From Customers";
da = new SqlDataAdapter(strSelect,nwindConn);
//SqlCommandBuilder custCB = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds,"Customers");
return ds.Tables["Customers"].DefaultView;
} public void BindGrid()
{
DataGrid1.DataSource = CreateTable();
DataGrid1.DataBind();
} private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = (int) e.Item.ItemIndex;
BindGrid();
} private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindGrid();
} private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//TextBox companyNameText = (TextBox)e.Item.Cells[2].Controls[0];
//TextBox addressText = (TextBox)e.Item.Cells[3].Controls[0];
string IDStr = e.Item.Cells[1].Text;
string companyNameStr = e.Item.Cells[2].Text
string addressStr = e.Item.Cells[3].Text;
string updateStr = "Update Customers Set CompanyName='"+companyNameStr+"',Address='"+addressStr+"' Where CustomerID='"+IDStr+"'";
SqlCommand myCommand = new SqlCommand(updateStr,nwindConn);
int debug=myCommand.ExecuteNonQuery();
DataGrid1.EditItemIndex = -1;
BindGrid();
}
1。程序无法更新,分步调试,显示如下结果:
e.Item.Cells[1].Text "" string
e.Item.Cells[2].Text "" string
e.Item.Cells[3].Text "" string
全是空值。想不通,请哪位兄弟指点一下,带我脱离苦海。
2。还有注释掉的两句//TextBox companyNameText = (TextBox)e.Item.Cells[2].Controls[0];
//TextBox addressText = (TextBox)e.Item.Cells[3].Controls[0];
如果不注释,提示说转换无效。
{
// 在此处放置用户代码以初始化页面
string myConnString = "workstation id=FREEWILLER;packet size=4096;user id=sa;data source=FREEWILLER;persist security info=True;initial catalog=Northwind;password=goodsf";
nwindConn = new SqlConnection(myConnString);
nwindConn.Open();
if(!Page.IsPostBack)
BindGrid();
} public ICollection CreateTable()
{
string strSelect = "Select Top 10 * From Customers";
da = new SqlDataAdapter(strSelect,nwindConn);
//SqlCommandBuilder custCB = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds,"Customers");
return ds.Tables["Customers"].DefaultView;
} public void BindGrid()
{
DataGrid1.DataSource = CreateTable();
DataGrid1.DataBind();
} private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = (int) e.Item.ItemIndex;
BindGrid();
} private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindGrid();
} private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//TextBox companyNameText = (TextBox)e.Item.Cells[2].Controls[0];
//TextBox addressText = (TextBox)e.Item.Cells[3].Controls[0];
string IDStr = e.Item.Cells[1].Text;
string companyNameStr = e.Item.Cells[2].Text
string addressStr = e.Item.Cells[3].Text;
string updateStr = "Update Customers Set CompanyName='"+companyNameStr+"',Address='"+addressStr+"' Where CustomerID='"+IDStr+"'";
SqlCommand myCommand = new SqlCommand(updateStr,nwindConn);
int debug=myCommand.ExecuteNonQuery();
DataGrid1.EditItemIndex = -1;
BindGrid();
}
1。程序无法更新,分步调试,显示如下结果:
e.Item.Cells[1].Text "" string
e.Item.Cells[2].Text "" string
e.Item.Cells[3].Text "" string
全是空值。想不通,请哪位兄弟指点一下,带我脱离苦海。
2。还有注释掉的两句//TextBox companyNameText = (TextBox)e.Item.Cells[2].Controls[0];
//TextBox addressText = (TextBox)e.Item.Cells[3].Controls[0];
如果不注释,提示说转换无效。
string addressText = ((TextBox)e.Item.Cells[2].Controls[1]).Text;
string UpdateCmd="update Customers set CompanyName='"+companyNameStr+"',Address='"+addressStr+"' Where CustomerID=@IDStr;OleDbCommand cmd=new OleDbCommand(UpdateCmd,conn);
cmd.Parameters.Add(new OleDbParameter("@IDStr",OleDbType.Integer,10));
cmd.Parameters["@IDStr"].Value=DataGrid1.DataKeys[e.Item.ItemIndex];
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string companyNameStr =((TextBox)e.Item.Cells[2].Controls[0]).Text;
string addressStr = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
string updateStr = "Update Customers Set CompanyName='"+companyNameStr+"',Address='"+addressStr+"' Where CustomerID=@IDStr";
SqlCommand myCommand = new SqlCommand(updateStr,nwindConn);
myCommand.Parameters.Add(new SqlParameter("@IDStr", SqlDbType.NVarChar, 5));
myCommand.Parameters["@IDStr"].Value = DataGrid1.DataKeys[e.Item.ItemIndex];
myCommand.ExecuteNonQuery();
DataGrid1.EditItemIndex = -1;
BindGrid();
}
错误提示:
1。e.Item.Cells[1].Controls[0].Text "ALFKI" string
可是如果直接在程序里写却是没有Text这个属性的。
2。IDStr 错误: 标识符“IDStr”超出范围
DataGrid1.DataKeys[e.Item.ItemIndex] 索引超出范围。必须为非负值并小于集合大小。
怎么做啊。