Click to See Complete Forum and Search --> : checkbox in datagrid


soniyakapoor03
March 6th, 2006, 11:27 PM
hi friends,


I new to .net.
please help.
i had a datagrid in c# asp.net.
in this datagrid i had a check box.
when i click on the check box,
i want to get the row id of the checked row.
is there any method in .net.


my code is like this

this.grdRoles.ItemCommand+=new DataGridCommandEventHandler(grdRoles_ItemCommand);

private void grdRoles_ItemCommand(object source, DataGridCommandEventArgs e)
{
int UID=Convert.ToInt32(drpUserName.SelectedItem.Value);
int RoleId = Convert.ToInt32((grdRoles.DataKeys[e.Item.ItemIndex]));
AdminSection objAdmin=new AdminSection();
objAdmin.AssignUserRoles(UID,RoleId);
}


but it is not entering int this code this code.
please help.

lakshmi_kasturi2006
March 7th, 2006, 05:51 AM
in aspx.cs file
SqlConnection con ;
SqlDataAdapter da;
DataSet ds;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
string cs= "Server=yourserver;uid=username;pwd=yourpassword;database=yourdatabasename;";

declare the above
before form_load



private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack){
con = new SqlConnection ();
con.ConnectionString =cs;
da = new SqlDataAdapter("select * from TableName",con);
ds = new DataSet();
da.Fill(ds,"modules");
DataGrid1.DataSource=ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}

}

public void ShowSelection(object sender, System.EventArgs e)
{
CheckBox chkSelected;
string s=null;

foreach (DataGridItem dgItem in DataGrid1.Items)
{
chkSelected = (CheckBox)dgItem.FindControl("CheckBox1");
if (chkSelected.Checked)
{
CheckBox chkTemp = chkSelected as CheckBox;
TableCell cell = chkTemp.Parent as TableCell;
DataGridItem item = cell.Parent as DataGridItem;
s+=(item.ItemIndex+1).ToString()+",";
}
}
if (s.Length > 0) {
s =s.Substring(0, s.Length - 1);
}

Label1.Text="The Selected Row ids:" +s;
}




in aspx page

<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 272px; POSITION: absolute; TOP: 64px"
runat="server" Width="378px" Height="72px">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox id="CheckBox1" runat="server" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid><asp:label id="Label1" style="Z-INDEX: 102; LEFT: 32px; POSITION: absolute; TOP: 464px" runat="server"
Width="296px" Height="32px">Label</asp:label>
<asp:Button id="Button1" OnClick="ShowSelection" style="Z-INDEX: 103; LEFT: 256px; POSITION: absolute; TOP: 256px" runat="server"
Width="216px" Height="32px" Text="Button"></asp:Button></form>