| |

VerySource

 Forgot password?
 Register
Search
View: 902|Reply: 6

How to implement editing function in DATAGRID, the project is difficult to do

[Copy link]

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-12-14 17:00:01
| Show all posts |Read mode
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.
Reply

Use magic Report

0

Threads

64

Posts

45.00

Credits

Newbie

Rank: 1

Credits
45.00

 China

Post time: 2020-12-14 17:30:01
| Show all posts
Is MESSAGE the ID of the Label?
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-12-14 20:45:01
| Show all posts
Probably not, this is the code copied from elsewhere. The functions of the above two functions have been modified after modification, but the modification of this always causes problems. There seems to be no text box.
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-14 22:15:01
| Show all posts
What kind of MESSAGE is yours? In the Web, it is best to use Script for prompt information
For example: Page.Response.Write("<script>alert('Error: A record with the same primary key already exists';)</script>")
If you want it to be displayed on the edit line, you can add a template column. Add a label to the template column to display error messages. example:
((Label)Grid.Items[i].Cells[j].Controls[0]).Text ="Error: A record with the same primary key already exists";
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-12-15 12:15:01
| Show all posts
Can I not allow it to modify the primary key, how to set it, and the Script mentioned above
For example: Page.Response.Write("<script>alert('Error: A record with the same primary key already exists';)</script>")
Is it added in HTML? ?
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-12-20 09:45:01
| Show all posts
Everyone, may I ask who has the editing source code for datagrid, and can you give me one.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-21 23:15:01
| Show all posts
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public class View: System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGridView;
private string connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

private void BindDataGrid()
{
SqlConnection cn = new SqlConnection(connectionString);
SqlCommand da = new SqlCommand( "Select * From Note", cn );
cn.Open();
DataGridView.DataSource = cmd.ExecuteReader();
DataGridView.DataKeyField = "id";
DataGridView.DataBind();
cn.Close();
}

private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack)
{
BindDataGrid();
}
}

#region Web Form Designer Generated Code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
To
private void InitializeComponent()
{
this.DataGridView.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler

(this.DataGridView_CancelCommand);
this.DataGridView.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler

(this.DataGridView_EditCommand);
this.DataGridView.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler

(this.DataGridView_UpdateCommand);
this.DataGridView.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler

(this.DataGridView_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DataGridView_EditCommand(object source, DataGridCommandEventArgs e)
{
DataGridView.EditItemIndex = e.Item.ItemIndex;
BindDataGrid();
}

private void DataGridView_CancelCommand(object source, DataGridCommandEventArgs e)
{
DataGridView.EditItemIndex = -1;
BindDataGrid();
}

private void DataGridView_UpdateCommand(object source, DataGridCommandEventArgs e)
{
TextBox txtName = (TextBox)e.Item.Cells[0].FindControl("txtName");
TextBox txtEmail = (TextBox)e.Item.Cells[1].FindControl("txtEmail");
TextBox txtNotes = (TextBox)e.Item.Cells[3].FindControl("txtNotes");

SqlConnection cn = new SqlConnection( connectionString);
string strUpdate = "Update Note set Username = @username, Email = @email, PostTime = @posttime, text = @text

Where id = @id";
SqlCommand cmd = new SqlCommand(strUpdate, cn );
cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = txtName.Text;
cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = txtEmail.Text;
cmd.Parameters.Add("@posttime", SqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add("@text", SqlDbType.Text).Value = txtNotes.Text;
cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = DataGridView.DataKeys[e.Item.ItemIndex].ToString();
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
DataGridView.EditItemIndex = -1;
BindDataGrid();
}

private void DataGridView_DeleteCommand(object source, DataGridCommandEventArgs e)
{
SqlConnection cn = new SqlConnection( connectionString);
string strDelete = "delete from Note Where id = @id";
SqlCommand cmd = new SqlCommand(strDelete, cn );
cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = DataGridView.DataKeys[e.Item.ItemIndex].ToString();
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
DataGridView.EditItemIndex = -1;
BindDataGrid();
}
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list