Click here to Skip to main content
16,013,642 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                        BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
                        CellPadding="3" CellSpacing="2" DataKeyNames="Qid"
                        DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand">
                        <Columns>
                            <asp:CommandField ShowSelectButton="True" />
                            <asp:BoundField DataField="Qid" HeaderText="Qid" InsertVisible="False"
                                ReadOnly="True" SortExpression="Qid" />
                            <asp:BoundField DataField="Question" HeaderText="Question"
                                SortExpression="Question" />
                            <asp:BoundField DataField="ans1" HeaderText="ans1" SortExpression="ans1" />
                            <asp:BoundField DataField="ans2" HeaderText="ans2" SortExpression="ans2" />
                            <asp:BoundField DataField="ans3" HeaderText="ans3" SortExpression="ans3" />
                            <asp:BoundField DataField="ans4" HeaderText="ans4" SortExpression="ans4" />
                            <asp:BoundField DataField="cans" HeaderText="cans" SortExpression="cans" />
                           <asp:TemplateField>
                           <ItemTemplate>
                               <asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
                           </ItemTemplate>
                           </asp:TemplateField>
                        </Columns>
                        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
                        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                        <SortedAscendingCellStyle BackColor="#FFF1D4" />
                        <SortedAscendingHeaderStyle BackColor="#B95C30" />
                        <SortedDescendingCellStyle BackColor="#F1E5CE" />
                        <SortedDescendingHeaderStyle BackColor="#93451F" />
                    </asp:GridView>
Posted
Comments
Meysam Toluie 10-Apr-14 1:29am    
Before asking these basic question, please google it! you will be surprise of many answer more faster.

 
Share this answer
 
C#

C#
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
 {
<pre lang="cs">String r = "INSERT INTO Table_name(invoiceno,srno, itmdescription, ) VALUES (@invoiceno,@srno, @itmdescription)";

cmd = new SqlCommand(r, cn);
cmd.Parameters.AddWithValue("@invoiceno", txt_invono.Text);
cmd.Parameters.AddWithValue("@srno", dataGridView1.Rows[i].Cells[0].Value);
cmd.Parameters.AddWithValue("@itmdescription",dataGridView1.Rows[i].Cells[1].Value);
cmd.executenonquery();
}
 
Share this answer
 
v2
.aspx page

XML
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager2" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="updpnl" runat="server">
        <ContentTemplate>
            <asp:GridView ID="grddata" runat="server" AutoGenerateColumns="false" OnRowUpdating="grddata_RowUpdating"
                OnRowEditing="grddata_RowEditing" OnRowDeleting="grddata_RowDeleting" OnRowCancelingEdit="grddata_RowCancelingEdit">
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <table>
                                <tr>
                                    <td>
                                        <asp:Label ID="lblid" Width="200px" runat="server" Text="Id"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblname" Width="200px" runat="server" Text="Name"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblsalary" runat="server" Width="200px" Text="Salary"></asp:Label>
                                    </td>
                                </tr>
                            </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table>
                                <tr>
                                    <td>
                                        <asp:Label ID="lblid" Width="200px" Text= runat="server"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblname" Width="200px" Text='<%# Eval("name") %>' runat="server"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblsalary" Width="200px" runat="server" Text='<%# Eval("salary") %>'></asp:Label>
                                    </td>
                                    <td>
                                        <asp:LinkButton ID="lnkedit" CommandName="Edit" OnClientClick="About.aspx.target='_blank';"
                                            Text="Edit" CommandArgument='<%# Eval("id") %>' runat="server"></asp:LinkButton>
                                    </td>
                                    <td>
                                        <asp:LinkButton ID="lnkdelete" Text="Delete" CommandName="Delete" CommandArgument='<%# Eval("id") %>'
                                            runat="server"></asp:LinkButton>
                                    </td>
                                    <td>
                                        <asp:LinkButton ID="redirect" runat="server" Text="Redirect" OnClientClick="About.aspx" target="_blank"></asp:LinkButton>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <tr>
                                <td>
                                    <asp:Label ID="lblid" Width="200px" Text='<%# Eval("id") %>' runat="server"></asp:Label>
                                </td>
                                <td>
                                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                                </td>
                                <td>
                                    <asp:TextBox ID="txtsalary" runat="server"></asp:TextBox>
                                </td>
                                <td>
                                    <asp:LinkButton ID="lnkupdate" CommandName="Update" Text="Update" CommandArgument='<%# Eval("id") %>'
                                        runat="server"></asp:LinkButton>
                                </td>
                                <td>
                                    <asp:LinkButton ID="lnkCancel" CommandName="Cancel" Text="Cancel" CommandArgument='<%# Eval("id") %>'
                                        runat="server"></asp:LinkButton>
                                </td>
                            </tr>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EmptyDataTemplate>
                    No Data Available
                </EmptyDataTemplate>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>



.cs page

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Net;
using System.Text;

public partial class Default2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"server=ADMIN-PC\NEERAJ;database=master;integrated security=true;");
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
showdata();
}
}
//private void PDF_Export()
//{
// Response.ContentType = "application/pdf";
// Response.AddHeader("content-disposition",
// "attachment;filename=GridViewExport.pdf");
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
// StringWriter sw = new StringWriter();
// HtmlTextWriter hw = new HtmlTextWriter(sw);
// GridView1.AllowPaging = false;
// GridView1.DataBind();
// GridView1.RenderControl(hw);
// StringReader sr = new StringReader(sw.ToString());
// Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
// HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
// PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
// pdfDoc.Open();
// htmlparser.Parse(sr);
// pdfDoc.Close();
// Response.Write(pdfDoc);
// Response.End();
//}
public void showdata()
{
SqlDataAdapter da=new SqlDataAdapter("select * from empdata",con);
DataSet ds=new DataSet();
da.Fill(ds);
grddata.DataSource=ds;
grddata.DataBind();
}
protected void grddata_RowEditing(object sender, GridViewEditEventArgs e)
{
//grddata.EditIndex = -1;
//Label name = (Label)grddata.Rows[0].Cells[1].FindControl("lblname");
//TextBox salary = (TextBox)grddata.Rows[0].Cells[2].FindControl("txtsalary");
grddata.EditIndex = e.NewEditIndex;
//LinkButton edit = (LinkButton)grddata.Rows[0].FindControl("lnkedit");
// //Response.Redirect("About.aspx?name=neeraj&salary=1200");
//string url = "About.aspx?name=neeraj&salary=1200";
//edit.OnClientClick = "window.open('" + url + "'); return false;";


}
protected void grddata_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//LinkButton edit = (LinkButton)grddata.Rows[0].FindControl("lnkedit");
Label name = (Label)grddata.Rows[e.RowIndex].FindControl("lblname");
//Label salary = (Label)grddata.Rows[0].FindControl("lblsal");
Label id = (Label)grddata.Rows[e.RowIndex].FindControl("lblid");
ViewState["id"] = id.Text;
//string id = grddata.DataKeys
// [e.Item.ItemIndex].ToString();

//SqlDataAdapter da = new SqlDataAdapter("UPDATE empdata SET name ='" + name + "' and salary=" + salary + " ", con);

SqlCommand cmd;


cmd = new SqlCommand("delete from empdata where id="+ViewState["id"]+" ", con);
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();

showdata();
con.Close();



}
protected void grddata_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grddata.EditIndex = -1;
showdata();
}
protected void grddata_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//string id = grddata.DataKeys[e.Item.ItemIndex].ToString();
TextBox name = (TextBox)grddata.Rows[0].FindControl("txtname");
TextBox salary = (TextBox)grddata.Rows[0].FindControl("txtsalary");
//Label id = (Label)grddata.Rows[e.RowIndex].FindControl("lblid.text");
//SqlDataAdapter da = new SqlDataAdapter("UPDATE empdata SET name ='" + name + "' and salary=" + salary + " ", con);
SqlCommand cmd;


cmd = new SqlCommand("UPDATE empdata SET name='" + name.Text + "',salary=" + salary.Text + " ", con);
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
grddata.EditIndex = -1;
showdata();
}
public void sendmail()
{
StringBuilder sb = new StringBuilder();

}

}
 
Share this answer
 
.cs page

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Net;
using System.Text;

public partial class Default2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"server=ADMIN-PC\NEERAJ;database=master;integrated security=true;");
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
showdata();
}
}
//private void PDF_Export()
//{
// Response.ContentType = "application/pdf";
// Response.AddHeader("content-disposition",
// "attachment;filename=GridViewExport.pdf");
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
// StringWriter sw = new StringWriter();
// HtmlTextWriter hw = new HtmlTextWriter(sw);
// GridView1.AllowPaging = false;
// GridView1.DataBind();
// GridView1.RenderControl(hw);
// StringReader sr = new StringReader(sw.ToString());
// Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
// HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
// PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
// pdfDoc.Open();
// htmlparser.Parse(sr);
// pdfDoc.Close();
// Response.Write(pdfDoc);
// Response.End();
//}
public void showdata()
{
SqlDataAdapter da=new SqlDataAdapter("select * from empdata",con);
DataSet ds=new DataSet();
da.Fill(ds);
grddata.DataSource=ds;
grddata.DataBind();
}
protected void grddata_RowEditing(object sender, GridViewEditEventArgs e)
{
//grddata.EditIndex = -1;
//Label name = (Label)grddata.Rows[0].Cells[1].FindControl("lblname");
//TextBox salary = (TextBox)grddata.Rows[0].Cells[2].FindControl("txtsalary");
grddata.EditIndex = e.NewEditIndex;
//LinkButton edit = (LinkButton)grddata.Rows[0].FindControl("lnkedit");
// //Response.Redirect("About.aspx?name=neeraj&salary=1200");
//string url = "About.aspx?name=neeraj&salary=1200";
//edit.OnClientClick = "window.open('" + url + "'); return false;";


}
protected void grddata_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//LinkButton edit = (LinkButton)grddata.Rows[0].FindControl("lnkedit");
Label name = (Label)grddata.Rows[e.RowIndex].FindControl("lblname");
//Label salary = (Label)grddata.Rows[0].FindControl("lblsal");
Label id = (Label)grddata.Rows[e.RowIndex].FindControl("lblid");
ViewState["id"] = id.Text;
//string id = grddata.DataKeys
// [e.Item.ItemIndex].ToString();

//SqlDataAdapter da = new SqlDataAdapter("UPDATE empdata SET name ='" + name + "' and salary=" + salary + " ", con);

SqlCommand cmd;


cmd = new SqlCommand("delete from empdata where id="+ViewState["id"]+" ", con);
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();

showdata();
con.Close();



}
protected void grddata_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grddata.EditIndex = -1;
showdata();
}
protected void grddata_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//string id = grddata.DataKeys[e.Item.ItemIndex].ToString();
TextBox name = (TextBox)grddata.Rows[0].FindControl("txtname");
TextBox salary = (TextBox)grddata.Rows[0].FindControl("txtsalary");
//Label id = (Label)grddata.Rows[e.RowIndex].FindControl("lblid.text");
//SqlDataAdapter da = new SqlDataAdapter("UPDATE empdata SET name ='" + name + "' and salary=" + salary + " ", con);
SqlCommand cmd;


cmd = new SqlCommand("UPDATE empdata SET name='" + name.Text + "',salary=" + salary.Text + " ", con);
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
grddata.EditIndex = -1;
showdata();
}
public void sendmail()
{
StringBuilder sb = new StringBuilder();

}

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900