Home > Forum > ASP.NET > i can't get databound value from Gridview Control i given Code below.. Please Help Me...

i can't get databound value from Gridview Control i given Code below.. Please Help Me...

by Thirumoorthy   on Feb 11, 2014   Category: ASP.NET  |  Views: 1554    |  Points: 5   |  Starter 
  Reply
.ASPX:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="P_ID"
OnRowDataBound="GRD_OnRowDataBound" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:BoundField HeaderText="P_ID" DataField="P_ID" ItemStyle-HorizontalAlign="Center" SortExpression="P_ID"
Visible="true">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="Modules" DataField="fModuleName" ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="APPROVE">
<ItemTemplate>
<asp:CheckBox runat="server" ID="P_Approve" AutoPostBack="false" Checked='<%# Convert.ToBoolean(Eval("P_Approve").ToString().Equals("1")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="INSERT">
<ItemTemplate>
<asp:CheckBox runat="server" ID="P_Insert" AutoPostBack="false" Checked='<%# Convert.ToBoolean(Eval("P_Add").ToString().Equals("1")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="VIEW">
<ItemTemplate>
<asp:CheckBox runat="server" ID="P_View" AutoPostBack="false" Checked='<%# Convert.ToBoolean(Eval("P_View").ToString().Equals("1")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UPDATE">
<ItemTemplate>
<asp:CheckBox runat="server" ID="P_Update" AutoPostBack="false" Checked='<%# Convert.ToBoolean(Eval("P_Edit").ToString().Equals("1")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DELETE">
<ItemTemplate>
<asp:CheckBox runat="server" ID="P_Delete" AutoPostBack="false" Checked='<%# Convert.ToBoolean(Eval("P_Delete").ToString().Equals("1")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>




.CS:
protected void UpdateBtn_Click(object sender, EventArgs e)
{
try
{
DataTable dt = new DataTable();
dt.Columns.Add("P_Approve", typeof(int));
dt.Columns.Add("P_Insert", typeof(int));
dt.Columns.Add("P_View", typeof(int));
dt.Columns.Add("P_Update", typeof(int));
dt.Columns.Add("P_Delete", typeof(int));
DataRow dr = dt.NewRow();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
string k = Convert.ToInt32(GridView1.Rows.Cells.ToString());
CheckBox a = (CheckBox)GridView1.Rows.FindControl("P_Approve");
CheckBox b = (CheckBox)GridView1.Rows.FindControl("P_Insert");
CheckBox c = (CheckBox)GridView1.Rows.FindControl("P_View");
CheckBox d = (CheckBox)GridView1.Rows.FindControl("P_Update");
CheckBox f = (CheckBox)GridView1.Rows.FindControl("P_Delete");
if (a.Checked == true){dr["P_Approve"] = 1;}else{dr["P_Approve"] = 0;}
if (b.Checked == true) { dr["P_Insert"] = 1; } else { dr["P_Insert"] = 0; }
if (c.Checked == true) { dr["P_View"] = 1; } else { dr["P_View"] = 0; }
if (d.Checked == true) { dr["P_Update"] = 1; } else { dr["P_Update"] = 0; }
if (f.Checked == true) { dr["P_Delete"] = 1; } else { dr["P_Delete"] = 0; }


if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand cmd = new SqlCommand("update tblPermissionDetails set P_Approve=@P_Approve,P_Add=@P_Add,P_View=@P_View,P_Edit=@P_Edit,P_Delete=@P_Delete where P_ID=@P_ID", con);
cmd.Parameters.AddWithValue("@P_ID",k);
cmd.Parameters.AddWithValue("@P_Approve",dr[0]);
cmd.Parameters.AddWithValue("@P_Add",dr[1]);
cmd.Parameters.AddWithValue("@P_View",dr[2]);
cmd.Parameters.AddWithValue("@P_Edit",dr[3]);
cmd.Parameters.AddWithValue("@P_Delete", dr[4]);
cmd.ExecuteNonQuery();
dt.Rows.Add(dr);
dr = dt.NewRow();
}
lblDesc.Text = "Record Updated Successfully.";
}
catch (Exception e1)
{

Response.Write(e1);
}


Bookmark and Share:
 
  User Reply  | Ask a question  |   Reply 
  Re :i can't get databound value from Gridview Control i given Code below.. Please Help Me...   
by Thamil
on Feb 11 2014 7:42AM
Points : 10
Gold 
hi thirumoorthi,

Make sure that you are binding the GridView in Not IsPostBack condition in page load event and you have not provided complete code in this post, Its better to provide clear code snippet and what kind of data you are looking in databound event.

Thanks

Recent Post

Latest Posts