Asked by:
Gridview to create

Question
-
User66371569 posted
HI people
I have created one table as following
create table Qualification(
empno varchar(20),
qualification nvarchar(100),
specialization nvarchar(300),
University nvarchar(400),
DFrom date,
Dto date,
Attachment nvarchar(300)
)what I want when I open page in visual studio
gridview shows
id 'auto serial number' empno ' textbox' qualification 'dropdownlist' specialization 'dropdownlist' University' dropdownlist' DFrom 'textbox' Dto textbox' Attachment 'file upload ADD ' button' delete 'button'
and let user write his details once press add it will save in database and show him in grideview what he wrote then he can delete or edit old one and also can add new row
Thank you so much
Sunday, January 13, 2019 11:01 AM
All replies
-
User475983607 posted
CRUD operation using a GridView is covered quite extensively in documentation.
At the bottom of the reference doc there are links to different GridView concepts. Start with the documentation and if you run into trouble post you source code.
Sunday, January 13, 2019 2:32 PM -
User66371569 posted
can anyone give me example readySunday, January 13, 2019 3:39 PM -
User475983607 posted
There are tons of examples in the posted link. Please take the time to go through the docs. It does not make sense to copy and paste the source code here.Sunday, January 13, 2019 3:47 PM -
User283571144 posted
Hi thepast,
According to your description and codes, I made a sample. It works well on my side, you could bind onRowCommand and onDatabind event to gridview.
More details about my test demo, you could refer to below codes:
<form id="form1" runat="server"> <div> <asp:GridView ID="autoserialnumber" runat="server" AutoGenerateColumns="false" OnRowCommand="autoserialnumber_RowCommand" OnRowDataBound="autoserialnumber_RowDataBound"> <Columns> <asp:TemplateField HeaderText="empno"> <ItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("empno") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField > <asp:TemplateField HeaderText="qualification"> <ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="specialization"> <ItemTemplate> <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="University"> <ItemTemplate> <asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="DFrom"> <ItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("DFrom") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Dto"> <ItemTemplate> <asp:TextBox ID="TextBox3" runat="server" Text='<%#Eval("Dto") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Button ID="Button1" runat="server" Text="edit" CommandName="edi" CommandArgument='<%#Eval("empno") %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Button ID="Button2" runat="server" Text="delete" CommandName="del" CommandArgument='<%#Eval("empno") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <table> <tr> <td>empno<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox></td> <td>qualification<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox></td> <td>specialization<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox></td> <td>University<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox> </td> <td>DFrom<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox></td> <td>Dto<asp:TextBox ID="TextBox9" runat="server"></asp:TextBox></td> </tr> <tr> <td> <asp:Button ID="ADD" runat="server" Text="ADD" OnClick="ADD_Click"/> </td> </tr> </table> </div> </form>
Code-behind:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { getData(); } } protected void getData() { string con = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=lilydata;Integrated Security=True;"; using (SqlConnection connect = new SqlConnection(con)) { connect.Open(); string sql = "select * from Qualification "; SqlDataAdapter sda = new SqlDataAdapter(sql, connect); DataTable dt = new DataTable(); sda.Fill(dt); this.autoserialnumber.DataSource = dt; this.autoserialnumber.DataBind(); } } protected void ADD_Click(object sender, EventArgs e) { string emp = TextBox4.Text; string qua = TextBox5.Text; string spe = TextBox6.Text; string uni = TextBox7.Text; string dfrom = TextBox8.Text; DateTime dfrom1 = DateTime.Parse(dfrom); string dto = TextBox9.Text; DateTime dto1 = DateTime.Parse(dto); string con = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=lilydata;Integrated Security=True;"; using (SqlConnection connect = new SqlConnection(con)) { connect.Open(); string sql = "insert into Qualification values(@emp,@qua,@spe,@uni,@dfrom,@dto)"; using (SqlCommand cmd = new SqlCommand(sql, connect)) { cmd.Parameters.AddWithValue("@emp", emp); cmd.Parameters.AddWithValue("@qua", qua); cmd.Parameters.AddWithValue("@spe", spe); cmd.Parameters.AddWithValue("@uni", uni); cmd.Parameters.AddWithValue("@dfrom", dfrom1); cmd.Parameters.AddWithValue("@dto", dto1); cmd.ExecuteNonQuery(); } } getData(); } protected void autoserialnumber_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "del") { string con = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=lilydata;Integrated Security=True;"; using (SqlConnection connect = new SqlConnection(con)) { connect.Open(); string sql = "delete from Qualification where empno=@emp"; SqlCommand cmd = new SqlCommand(sql, connect); cmd.Parameters.AddWithValue("@emp", e.CommandArgument.ToString()); cmd.ExecuteNonQuery(); getData(); } } if (e.CommandName == "edi") { GridViewRow row1 = ((Control)e.CommandSource).BindingContainer as GridViewRow; var a = row1.FindControl("TextBox1") as TextBox; string emp = a.Text; var b = row1.FindControl("DropDownList1") as DropDownList; string qua = b.SelectedValue; var c = row1.FindControl("DropDownList2") as DropDownList; string spe = c.SelectedValue; var d = row1.FindControl("DropDownList3") as DropDownList; string uni = d.SelectedValue; var f = row1.FindControl("TextBox2") as TextBox; string dfrom = f.Text; var g = row1.FindControl("TextBox3") as TextBox; string dto = g.Text; string sql = "update Qualification set qualification=@qua,specialization=@spec,University=@univers,Dfrom=@dfr,Dto=@dto where empno=@emp"; string con = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=lilydata;Integrated Security=True;"; using (SqlConnection connect2 = new SqlConnection(con)) { connect2.Open(); SqlCommand cmd2 = new SqlCommand(sql,connect2); cmd2.Parameters.AddWithValue("@emp",e.CommandArgument.ToString()); cmd2.Parameters.AddWithValue("@qua", qua); cmd2.Parameters.AddWithValue("@spec", spe); cmd2.Parameters.AddWithValue("@univers", uni); cmd2.Parameters.AddWithValue("@dfr", dfrom); cmd2.Parameters.AddWithValue("@dto", dto); cmd2.ExecuteNonQuery(); getData(); } } } protected void autoserialnumber_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DropDownList d1 = e.Row.FindControl("DropDownList1") as DropDownList; DropDownList d2 = e.Row.FindControl("DropDownList2") as DropDownList; DropDownList d3 = e.Row.FindControl("DropDownList3") as DropDownList; string con = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=lilydata;Integrated Security=True;"; using (SqlConnection connect = new SqlConnection(con)) { connect.Open(); string sql = "select qualification from Qualification where empno=@emp "; string emp = (e.Row.FindControl("TextBox1") as TextBox).Text; SqlCommand cmd = new SqlCommand(sql,connect); cmd.Parameters.AddWithValue("@emp",emp); d1.DataSource = cmd.ExecuteScalar(); d1.DataBind(); string sql1 = "select specialization from Qualification where empno=@emp "; SqlCommand cmd1 = new SqlCommand(sql1,connect); cmd1.Parameters.AddWithValue("@emp", emp); d2.DataSource = cmd1.ExecuteScalar(); d2.DataBind(); string sql2 = "select University from Qualification where empno=@emp"; SqlCommand cmd2 = new SqlCommand(sql2,connect); cmd2.Parameters.AddWithValue("@emp", emp); d3.DataSource = cmd2.ExecuteScalar(); d3.DataBind(); } } }
Result:
Best Regards,
Brando
Tuesday, January 15, 2019 8:44 AM -
User1341756031 posted
Full source code of...GridView Add, Edit and Delete
Tuesday, January 15, 2019 9:41 AM