| 
 | 
 
Dear prawns, how to implement the editing function in DATAGRID, I used C#, and found some information, but the function is still not realized. Below is my code. May I ask how I should modify it. Thank you very much. 
 
public void DataGrid1_EditCommand(Object sender, DataGridCommandEventArgs E) 
{ 
DataGrid1.EditItemIndex = (int)E.Item.ItemIndex; 
 
LoadDatebase();//Open the database 
string MyComStr="select pclass_id,pclass_name from pclass"; 
sqlCommand1=new SqlCommand(MyComStr,this.myConnect); 
sqlDataAdapter1=new SqlDataAdapter(); 
sqlDataAdapter1.SelectCommand=sqlCommand1; 
sqlDataAdapter1.Fill(ClassDaSet,"p_signup"); 
DataGrid1.DataSource=ClassDaSet.Tables["p_signup"].DefaultView; 
DataGrid1.DataBind(); 
To 
} 
 
public void DataGrid1_CancelCommand(Object sender, DataGridCommandEventArgs E) 
{ 
DataGrid1.EditItemIndex = -1; 
 
LoadDatebase();//Open the database 
string MyComStr="select pclass_id,pclass_name from pclass"; 
sqlCommand1=new SqlCommand(MyComStr,this.myConnect); 
sqlDataAdapter1=new SqlDataAdapter(); 
sqlDataAdapter1.SelectCommand=sqlCommand1; 
sqlDataAdapter1.Fill(ClassDaSet,"p_signup"); 
DataGrid1.DataSource=ClassDaSet.Tables["p_signup"].DefaultView; 
DataGrid1.DataBind(); 
} 
 
public void DataGrid1_UpdateCommand(Object sender, DataGridCommandEventArgs E) 
{ 
//string MyComStr="select pclass_id,pclass_name from pclass"; 
String updateCmd = "UPDATE pclass SET pclass_id = @Id, pclass_name = @Name"; 
 
SqlCommand myCommand = new SqlCommand(updateCmd, myConnection); 
 
myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NChar, 1)); 
myCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar, 50)); 
; 
 
myCommand.Parameters["@Id"].Value = DataGrid1.DataKeys[(int)E.Item.ItemIndex]; 
 
String[] cols = {"@Id","@Name"}; 
 
int numCols = E.Item.Cells.Count; 
for (int i=1; i<numCols-1; i++) //Skip the first, second and last columns 
{ 
String colvalue =((TextBox)E.Item.Cells[i].Controls[0]).Text; 
 
// Check if there is a null value in the required field 
if (colvalue == "") 
{ 
Message.InnerHtml = "Error: "Category Name" does not allow null values"; 
Message.Style["color"] = "red"; 
return; 
} 
 
myCommand.Parameters[cols[i-1]].Value = colvalue; 
} 
 
//Append the last line and convert the true/false value to 0/1 
if (String.Compare(((TextBox)E.Item.Cells[numCols-1].Controls[0]).Text, "True", true)==0) 
myCommand.Parameters["@Contract"].Value = "1"; 
else 
myCommand.Parameters["@Contract"].Value = "0"; 
 
myCommand.Connection.Open(); 
 
try 
{ 
myCommand.ExecuteNonQuery(); 
Message.InnerHtml = "<b>Record has been updated</b><br>" + updateCmd; 
MyDataGrid.EditItemIndex = -1; 
} 
catch (SqlException e) 
{ 
if (e.Number == 2627) 
Message.InnerHtml = "Error: A record with the same primary key already exists"; 
else 
Message.InnerHtml = "Error: Failed to update the record, please make sure to fill in the fields correctly"; 
Message.Style["color"] = "red"; 
} 
 
myCommand.Connection.Close(); 
 
LoadDatebase();//Open the database 
string MyComStr="select pclass_id,pclass_name from pclass"; 
sqlCommand1=new SqlCommand(MyComStr,this.myConnect); 
sqlDataAdapter1=new SqlDataAdapter(); 
sqlDataAdapter1.SelectCommand=sqlCommand1; 
sqlDataAdapter1.Fill(ClassDaSet,"p_signup"); 
DataGrid1.DataSource=ClassDaSet.Tables["p_signup"].DefaultView; 
DataGrid1.DataBind(); 
} 
To 
There is a problem with the old prompt "MESSAGE", please tell me how should I define MESSAGE, and please take a look at it. What is the problem with the code, it cannot be modified or updated, dizzy, I am a little bit embarrassed when I first arrived, the question is naive , Can be resolved very grateful. |   
 
 
 
 |