Reputation: 1905
Hi i have a grid view which has a checkbox column...
when ever the user checks and clikcs on the button this is how my grid Looks
checkboxColumn|ID|CustomerNAme
--------------------------------------
1|asjd
2|asdfas
3|asdjka
assign(Button)
When user checks the required rows i want to get only ID value of grid and stoer into database
protected void BtnAssignWork_Click(object sender, EventArgs e)
{
for (int i = 0; i < GrdCustomerData.RowsInViewState.Count; i++)
{
GridDataControlFieldCell cell = GrdCustomerData.RowsInViewState[i].Cells[0] as GridDataControlFieldCell;
CheckBox chk = cell.FindControl("Chkselect") as CheckBox;
if (chk.Checked == true)
{
var allIDs = (from item in GrdCustomerData.Rows.Cast<GridRow>()
let id = int.Parse(item.Cells[1].Text)
where chk.Checked
select id).ToList();
using (var myconn = new SqlConnection(connstring))
{
foreach (var id in allIDs)
{
using (var mycomm = new SqlCommand("SP_AssignedWork", myconn))
{
myconn.Open();
mycomm.CommandType = CommandType.StoredProcedure;
mycomm.Parameters.Add("@LoanID", SqlDbType.VarChar).Value = id;
mycomm.Parameters.Add("@EmpID", SqlDbType.VarChar).Value = DDLSelectEmployee.SelectedValue;
mycomm.ExecuteNonQuery();
myconn.Close();
}
}
}
}
can any one help me with this....
Upvotes: 1
Views: 12901
Reputation: 1905
Any ways i solved my answer....
here is the code i hope it may be helpfull to some one....
void grid1_RowDataBound(object sender, GridRowEventArgs e)
{
if (e.Row.RowType == GridRowType.DataRow && GrdCustomerData.RowsInViewState.Count > 0)
{
GridDataControlFieldCell cell = e.Row.Cells[0] as GridDataControlFieldCell;
CheckBox chk = cell.FindControl("Chkselect") as CheckBox;
GridDataControlFieldCell cellInViewState = GrdCustomerData.RowsInViewState[e.Row.RowIndex].Cells[0] as GridDataControlFieldCell;
CheckBox chkInViewState = cellInViewState.FindControl("Chkselect") as CheckBox;
if (cell.Value == chkInViewState.ToolTip)
{
chk.Checked = chkInViewState.Checked;
}
}
}
protected void BtnAssignWork_Click(object sender, EventArgs e)
{
string LoanIds = "";
for (int i = 0; i < GrdCustomerData.RowsInViewState.Count; i++)
{
GridDataControlFieldCell cell = GrdCustomerData.RowsInViewState[i].Cells[0] as GridDataControlFieldCell;
CheckBox chk = cell.FindControl("Chkselect") as CheckBox;
if (chk.Checked == true)
{
if (!string.IsNullOrEmpty(LoanIds))
LoanIds += "";
LoanIds = chk.ToolTip;
SqlConnection myconn = new SqlConnection(connstring);
SqlCommand mycomm = new SqlCommand("SP_AssignedWork", myconn);
myconn.Open();
mycomm.CommandType = CommandType.StoredProcedure;
mycomm.Parameters.Add("@LoanID", SqlDbType.VarChar).Value = LoanIds;
mycomm.Parameters.Add("@EmpID", SqlDbType.VarChar).Value = DDLSelectEmployee.SelectedValue;
mycomm.ExecuteNonQuery();
myconn.Close();
}
}
}
}
Upvotes: 1
Reputation: 460340
Assuming you're using a TemplateField
and a CheckBox
with ID="Chkselect"
:
protected void BtnAssignWork_Click(object sender, EventArgs e){
var allIDs = (from item in GrdCustomerData.Rows.Cast<GridViewRow>()
let chk = (CheckBox)item.Cells[0].FindControl("Chkselect")
let id = int.Parse(item.Cells[1].Text)
where chk.Checked
select id).ToList();
using(var myconn = new SqlConnection(connstring)){
myconn.Open();
foreach (var id in allIDs) {
using(var mycomm = new SqlCommand("SP_AssignedWork", myconn)){
mycomm.CommandType = CommandType.StoredProcedure;
mycomm.Parameters.Add("@LoanID", SqlDbType.Int).Value = id;
mycomm.Parameters.Add("@EmpID", SqlDbType.Int).Value = int.Parse(DDLSelectEmployee.SelectedValue);
mycomm.ExecuteNonQuery();
}
}
}
}
Edit: Just for clarification, i've joinded together both parts. You don't need an additional row-loop since the LINQ query already does.
Upvotes: 1
Reputation: 79979
You can use the SelectedIndex
property to get the currently selected row, then get the cell you want from that row. Something like:
GridViewRow row = CustomersGridView.SelectedRow;
int CustomerId = int.parse(row.Cells[2].Tex);
Upvotes: 1
Reputation: 486
Use Gridviewrows class to find the current row from that use cell[1] which represent the control at that cell from that u can get the id
Upvotes: 0