My code behind is as follows......................................................
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
databind();
}
}
public void databind()
{
var nw = ConfigurationManager.ConnectionStrings["nw"];
var connection = new SqlConnection();
connection.ConnectionString = nw.ConnectionString;
//-----------------------Commad type as simple SQL Query
var cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * From Employee_Details";
//-----------------------Command type as Stored Procedure
//var cmd = connection.CreateCommand();
//cmd.CommandType = CommandType.StoredProcedure;
//cmd.CommandText = "GetAllEmployeeStatus";
//-----------------------Display two tables record in one gridview with SQL Query having same ID Column in both table using Inner Join
//var cmd = connection.CreateCommand();
//cmd.CommandType = CommandType.Text;
//cmd.CommandText = "SELECT E1.First_Name + ' ' + E1.Last_Name AS [Full Name], E2.Department, E2.City, E1.Mobile FROM Employee_Details AS E1 INNER JOIN Employee_Status AS E2 ON E1.Id = E2.Id";
//GridView1.DataKeys[e.RowIndex].Values[0].ToString();
int x = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
Label1.Text = x.ToString();
delete(x);
}
public void delete(int EmpID)
{
var nw = ConfigurationManager.ConnectionStrings["nw"];
var connection = new SqlConnection();
connection.ConnectionString = nw.ConnectionString;
Can you tell me please how should i make use of e.commandargument while for deleting row. And Is there is no need for GridView1_RowDeleting event after that......?
Can you tell me whether my delete command will be ok...?or is any mistake on that....?
Can you tell me please how should i make use of e.commandargument while for deleting row. And Is there is no need for GridView1_RowDeleting event after that......?
Hi,
You don't need to handle the event of GridView_RowDeleting,because since you are using e.CommandArgument,in fact,you can use Eval to bind to the property with the primary column of the table,and then you can handle the event of GridView_RowCommand to delete
the specified row with the help of SqlCommand.
I want to remove a particular row that will be seleted by user in gridview....how i can that...?
Hi,
If you wanna remove a particular row after selecting it first,and you can try these steps:
1)Make sure that there's a Button, ImageButton or LinkButton whose CommandName is "Select".
2)Make sure that there's a common button out of GridView,and when selecting a Row,you can just write down something like this in the button's click event:
int x = Convert.ToInt32(GridView1.SelectedRow.DataKeys[0]);
Ritesh Gupta
Member
1 Points
13 Posts
How should i Delete Rows from my gridview seleted by using Templated field image control and usin...
Jan 01, 2013 02:37 AM|LINK
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowcancelingedit="GridView1_RowCancelingEdit" DataKeyNames="Id"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="true" />
<asp:TemplateField HeaderText="Employee First Name">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# Bind("First_Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("First_Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Last Name">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# Bind("Last_Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("Last_Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Mobile Number">
<ItemTemplate>
<asp:Label ID="lblMobileNumber" runat="server" Text='<%# Bind("Mobile") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMobileNumber" runat="server" Text='<%# Bind("Mobile") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ID="btnEdit" runat="server" CommandName="Edit" ImageUrl="~/Images/Edit.png" ToolTip="Edit" AutoPostBack="True"/>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="btnUpdate" runat="server" CommandName="Update" ImageUrl="~/Images/Update.png" ToolTip="Update" AutoPostBack="True" />
<span onclick="return confirm('Are you sure to Delete the record?')">
<asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete" ImageUrl="~/Images/Delete.png" ToolTip="Delete" AutoPostBack="True" />
</span>
<asp:ImageButton ID="btnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.png" ToolTip="Cancel" AutoPostBack="True" />
</EditItemTemplate>
</asp:TemplateField>
<%-- <asp:BoundField DataField="Department" HeaderText="Department" />
<asp:TemplateField HeaderText="Full Name">
<ItemTemplate>
<%#Eval("First_Name") %> <%#Eval("Last_Name") %>
</ItemTemplate>
</asp:TemplateField>--%>
</Columns>
</asp:Gridviw>
My code behind is as follows......................................................
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
databind();
}
}
public void databind()
{
var nw = ConfigurationManager.ConnectionStrings["nw"];
var connection = new SqlConnection();
connection.ConnectionString = nw.ConnectionString;
//-----------------------Commad type as simple SQL Query
var cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * From Employee_Details";
//-----------------------Command type as Stored Procedure
//var cmd = connection.CreateCommand();
//cmd.CommandType = CommandType.StoredProcedure;
//cmd.CommandText = "GetAllEmployeeStatus";
//-----------------------Display two tables record in one gridview with SQL Query having same ID Column in both table using Inner Join
//var cmd = connection.CreateCommand();
//cmd.CommandType = CommandType.Text;
//cmd.CommandText = "SELECT E1.First_Name + ' ' + E1.Last_Name AS [Full Name], E2.Department, E2.City, E1.Mobile FROM Employee_Details AS E1 INNER JOIN Employee_Status AS E2 ON E1.Id = E2.Id";
//var cmd = connection.CreateCommand();
//cmd.CommandType = CommandType.Text;
//cmd.CommandText = "SELECT First_Name + ' ' + Last_Name AS [Full Name] From Employee_Details";
var da = new SqlDataAdapter(cmd);
var ds = new DataSet("MydataSet");
da.Fill(ds, "Employee_Details");
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
databind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
databind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//GridView1.DataKeys[e.RowIndex].Values[0].ToString();
int x = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
Label1.Text = x.ToString();
delete(x);
}
public void delete(int EmpID)
{
var nw = ConfigurationManager.ConnectionStrings["nw"];
var connection = new SqlConnection();
connection.ConnectionString = nw.ConnectionString;
//var cmd = connection.CreateCommand();
//cmd.CommandType = CommandType.Text;
//cmd.CommandText = "Select * From Employee_Details";
var ds = new DataSet("MyDataset");
//var da = new SqlDataAdapter(cmd);
var command = connection.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = "DELETE FROM Employee_Details WHERE Id = @Id";
SqlParameter param = command.CreateParameter();
param.ParameterName = "@Id";
param.DbType = DbType.Int32;
param.Value = EmpID;
var da1 = new SqlDataAdapter(command);
da1.DeleteCommand = command;
//da1.Fill(ds);
da1.Update(ds);
//GridView1.DataSource = ds;
//GridView1.DataBind();
databind();
Label1.Text = "Ok";
}
It gives me error like that as follows
Update unable to find TableMapping['Table'] or DataTable 'Table'.
Please help me for that.............
anuj_koundal
Contributor
2269 Points
541 Posts
Re: How should i Delete Rows from my gridview seleted by using Templated field image control and ...
Jan 01, 2013 05:35 AM|LINK
try this
add command argument to the image button and pass id to it (PK)
CommandArgument='<%#Eval("id") %>'
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { switch (e.CommandName) { case "Edit": //Your edit code//You can access your primary key as : e.CommandArgument break; case "Delete": //Your delete code break; case "Update": //Your Update Code break; } }Anuj Koundal
My Blog
Mark as Answer on the post that helps you.
Ritesh Gupta
Member
1 Points
13 Posts
Re: How should i Delete Rows from my gridview seleted by using Templated field image control and ...
Jan 01, 2013 01:36 PM|LINK
Thank you...
Can you tell me please how should i make use of e.commandargument while for deleting row. And Is there is no need for GridView1_RowDeleting event after that......?
Can you tell me whether my delete command will be ok...?or is any mistake on that....?
Please tell me.....
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How should i Delete Rows from my gridview seleted by using Templated field image control and ...
Jan 02, 2013 12:49 AM|LINK
Hi,
You don't need to handle the event of GridView_RowDeleting,because since you are using e.CommandArgument,in fact,you can use Eval to bind to the property with the primary column of the table,and then you can handle the event of GridView_RowCommand to delete the specified row with the help of SqlCommand.
For more you can refer to this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowcommand="GridView1_RowCommand"> <Columns> <asp:TemplateField> <ItemTemplate> Data : <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label> <asp:LinkButton ID="btnDelete" runat="server" CommandArgument='<%# Eval("No") %>' CommandName="Del">Delete</asp:LinkButton> <asp:LinkButton ID="btnShow" runat="server" CommandArgument='<%# Eval("No") %>' CommandName="Show">Show</asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>Code-behind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Del") { //Take out argument and do deleting } }Ritesh Gupta
Member
1 Points
13 Posts
Re: How should i Delete Rows from my gridview seleted by using Templated field image control and ...
Jan 02, 2013 07:13 AM|LINK
How i can find which row will be seleted in gridview1_RowCommand()...?
my code is as below but it will remove all rows from gridview.
string command = e.CommandName;
string id = e.CommandArgument.ToString();
switch (command)
{
case "Delete":
//int x = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
Response.Write("<b>Marked deleted for" + id + "button clicked</b>");
delete(int.Parse(id));
databind();
break;
}
public void delete(int EmpID)
{
var nw = ConfigurationManager.ConnectionStrings["nw"];
var connection = new SqlConnection();
connection.ConnectionString = nw.ConnectionString;
var command = connection.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = "DELETE FROM Employee_Details WHERE Id=@Id";
SqlParameter idparameter = new SqlParameter();
idparameter.ParameterName = "@Id";
idparameter.SqlDbType = SqlDbType.Int;
idparameter.Value = EmpID;
command.Parameters.Add(idparameter);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
I want to remove a particular row that will be seleted by user in gridview....how i can that...?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How should i Delete Rows from my gridview seleted by using Templated field image control and ...
Jan 02, 2013 07:26 AM|LINK
Hello,
Your codes look nice,and your "x" is a primary key。And it should only remove the primary record.
And then please use a new SqlDataAdapter to Fill into a DataTable and re-databind to the GridView.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How should i Delete Rows from my gridview seleted by using Templated field image control and ...
Jan 02, 2013 07:32 AM|LINK
Hi,
If you wanna remove a particular row after selecting it first,and you can try these steps:
1)Make sure that there's a Button, ImageButton or LinkButton whose CommandName is "Select".
2)Make sure that there's a common button out of GridView,and when selecting a Row,you can just write down something like this in the button's click event:
int x = Convert.ToInt32(GridView1.SelectedRow.DataKeys[0]);
And then use SqlCommand to cope with the problem.