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
Jes Sie
742
1.2k
280.1k
Saving Checked Value From a Gridview With Paging to Database
Mar 26 2020 4:10 AM
Hello again,
I have a grid view with paging. I need to save the selected values to my database. I was able to retain the checkbox value while paging. My problem is that, when I save it to the database, it saves only the checked value of the first page of the grid view and not the succeeding pages. Please refer to my code snippets in retaining the checked vaue below:
private
void
SaveCheckedValue()
{
ArrayList clauseIDs =
new
ArrayList();
int
index = -1;
foreach
(GridViewRow row
in
gvClauses.Rows)
{
index = (
int
)gvClauses.DataKeys[row.RowIndex].Value;
bool
result = ((CheckBox)row.FindControl(
"cbSelect"
)).Checked;
if
(Session[
"Selected"
] !=
null
)
{
clauseIDs = (ArrayList)Session[
"Selected"
];
}
if
(result)
{
if
(!clauseIDs.Contains(index))
{
clauseIDs.Add(index);
}
}
else
{
clauseIDs.Remove(index);
}
}
if
(clauseIDs !=
null
&& clauseIDs.Count > 0)
{
Session[
"Selected"
] = clauseIDs;
}
}
private
void
GetCheckedValue()
{
ArrayList clauseIDs = (ArrayList)Session[
"Selected"
];
if
(clauseIDs !=
null
&& clauseIDs.Count > 0)
{
foreach
(GridViewRow row
in
gvClauses.Rows)
{
int
index = (
int
)gvClauses.DataKeys[row.RowIndex].Value;
if
(clauseIDs.Contains(index))
{
CheckBox cbSelect = (CheckBox)row.FindControl(
"cbSelect"
);
cbSelect.Checked =
true
;
}
}
}
}
Below is how I save to the database:
private
void
saveCheckedItem()
{
foreach
(GridViewRow row
in
gvClauses.Rows)
{
if
(row.RowType == DataControlRowType.DataRow)
{
CheckBox clausecode = (CheckBox)row.FindControl(
"cbSelect"
);
if
(clausecode !=
null
&& clausecode.Checked)
{
Label lblClauseCode = (Label)row.FindControl(
"lblClauseCode"
);
using
(SqlConnection con = DBCS.DBCon())
{
SqlCommand cmd =
new
SqlCommand(
"spNonMotor_InsertQuotationClause"
, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(
"@QuotationNo"
, txtQuotationNo.Text.Trim());
cmd.Parameters.AddWithValue(
"@ClauseCode"
, lblClauseCode.Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
}
}
}
}
}
Hope someone can guide me on how to do it. Thanks in advance.
Reply
Answers (
1
)
Entity Framework - Validations
mvc cutom role provider suddenly stopped working