TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Debasis Mohapatra
NA
381
56.6k
Dynamically Adding and Deleting Rows in GridView and Saving
Dec 16 2019 4:26 AM
Hi,
After click the Addnew button in Gridview the data is adding . i have a drop down box after that when i have enter the Dynamically Adding the rows the data is adding now the question is how to save the data in after click the save button.
My code
private
void
SetPreviousData()
{
int
rowIndex = 0;
if
(ViewState[
"CurrentTable"
] !=
null
)
{
DataTable dt = (DataTable)ViewState[
"CurrentTable"
];
if
(dt.Rows.Count > 0)
{
for
(
int
i = 0; i < dt.Rows.Count; i++)
{
DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl(
"ddlhead"
);
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl(
"txtamount"
);
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl(
"txtremark"
);
box1.Text = dt.Rows[i][
"Column1"
].ToString();
box2.Text = dt.Rows[i][
"Column2"
].ToString();
box3.Text = dt.Rows[i][
"Column3"
].ToString();
rowIndex++;
}
}
}
protected
void
Gridview1_SelectedIndexChanged(
object
sender, EventArgs e)
{
}
protected
void
btnsave_Click(
object
sender, EventArgs e)
{
string
sql =
string
.Empty;
try
{
if
(
string
.IsNullOrEmpty(hdnid.Value.ToString()))
sql =
"insert into payment (vessel_id,head,amount,remark) values ('"
+ ddlvessal.SelectedValue.ToString() +
"',)"
;
int
k = General.ExecuteQuery(sql);
}
catch
(Exception ex)
{
// throw;
}
finally
{ }
}
}
}
private
void
SetInitialRow()
{
DataTable dt =
new
DataTable();
DataRow dr =
null
;
dt.Columns.Add(
new
DataColumn(
"RowNumber"
,
typeof
(
string
)));
dt.Columns.Add(
new
DataColumn(
"Column1"
,
typeof
(
string
)));
dt.Columns.Add(
new
DataColumn(
"Column2"
,
typeof
(
string
)));
dt.Columns.Add(
new
DataColumn(
"Column3"
,
typeof
(
string
)));
dt.Columns.Add(
new
DataColumn(
"Action"
,
typeof
(
string
)));
dr = dt.NewRow();
dr[
"RowNumber"
] = 1;
dr[
"Column1"
] =
string
.Empty;
dr[
"Column2"
] =
string
.Empty;
dr[
"Column3"
] =
string
.Empty;
dt.Rows.Add(dr);
//dr = dt.NewRow();
//Store the DataTable in ViewState
ViewState[
"CurrentTable"
] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
private
void
AddNewRowToGrid()
{
int
rowIndex = 0;
if
(ViewState[
"CurrentTable"
] !=
null
)
{
DataTable dtCurrentTable = (DataTable)ViewState[
"CurrentTable"
];
DataRow drCurrentRow =
null
;
if
(dtCurrentTable.Rows.Count > 0)
{
for
(
int
i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl(
"ddlhead"
);
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl(
"txtamount"
);
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl(
"txtremark"
);
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow[
"RowNumber"
] = i + 1;
dtCurrentTable.Rows[i - 1][
"Column1"
] = box1.Text;
dtCurrentTable.Rows[i - 1][
"Column2"
] = box2.Text;
dtCurrentTable.Rows[i - 1][
"Column3"
] = box3.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState[
"CurrentTable"
] = dtCurrentTable;
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write(
"ViewState is null"
);
}
//Set Previous Data on Postbacks
SetPreviousData();
}
protected
void
Gridview1_SelectedIndexChanged(
object
sender, EventArgs e)
{
}
protected
void
btnsave_Click(
object
sender, EventArgs e)
{
string
sql =
string
.Empty;
try
{
if
(
string
.IsNullOrEmpty(hdnid.Value.ToString()))
sql =
"insert into payment (vessel_id,head,amount,remark) values ('"
+ ddlvessal.SelectedValue.ToString() +
"',)"
;
int
k = General.ExecuteQuery(sql);
}
catch
(Exception ex)
{
// throw;
}
finally
{ }
}
}
}
Design
<div
class
=
"body"
>
<asp:gridview ID=
"Gridview1"
runat=
"server"
ShowFooter=
"true"
AutoGenerateColumns=
"false"
OnSelectedIndexChanged=
"Gridview1_SelectedIndexChanged"
style=
"border-collapse:collapse;"
class
=
"table table-stripped table-bordered"
>
<Columns>
<asp:BoundField DataField=
"RowNumber"
HeaderText=
"SL No"
/>
<asp:TemplateField HeaderText=
" Head"
>
<ItemTemplate>
<asp:DropDownList ID=
"ddlhead"
class
=
"form-control show-tick"
data-live-search=
"true"
runat=
"server"
DataSourceId=
"MyDataSource"
DataTextField=
"name"
DataValueField=
"name"
></asp:DropDownList>
<asp:HiddenField ID=
"hdnid"
runat=
"server"
/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=
"Amount"
>
<ItemTemplate>
<asp:TextBox ID=
"txtamount"
runat=
"server"
></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=
"Remark"
>
<ItemTemplate>
<asp:TextBox ID=
"txtremark"
runat=
"server"
></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=
"Action"
>
<ItemTemplate>
<div
class
=
"btn-group"
>
<asp:LinkButton ID=
"lnkaddnamedelete"
data-toggle=
"tooltip"
CommandArgument=
"Delete"
OnClientClick=
"javascript:return confirm('Are you sure you want to delete this ?');"
CausesValidation=
"false"
data-placement=
"bottom"
title=
"Delete"
CssClass=
"fa fa-close"
runat=
"server"
onclick=
"lnkaddnamedelete_Click"
></asp:LinkButton>
</div>
</ItemTemplate>
<FooterStyle HorizontalAlign=
"Right"
/>
<FooterTemplate>
<asp:Button ID=
"ButtonAdd"
class
=
"btn btn-success"
CausesValidation=
"false"
OnClick=
"ButtonAdd_Click"
runat=
"server"
Text=
"Add New"
></asp:Button>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
Please how to save the all the data in the database after click on save
string
sql =
string
.Empty;
try
{
if
(
string
.IsNullOrEmpty(hdnid.Value.ToString()))
sql =
"insert into payment (vessel_id,head,amount,remark) values ('"
+ ddlvessal.SelectedValue.ToString() +
"',)"
;
int
k = General.ExecuteQuery(sql);
}
catch
(Exception ex)
{
// throw;
}
finally
{ }
Reply
Answers (
3
)
Devexpress timer
Design Pattern in C#