C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




checkboxes in a gridview


Posted Date: 30 Jul 2008      Total Responses: 4

Posted By: prashanth       Member Level: Bronze     Points: 1


i have a gridview in that one cloumn is having checkboxes so whenver i want to save a new record in to database it should be saved if the checkbox.checked=True otherwise it won't be saved .so anybody pls send the code



Responses

Author: nirmala    30 Jul 2008Member Level: GoldRating:     Points: 6
haii,this code is for selected checkboxex in gridview and also delete:

<asp:Button ID="btndelete" runat="server" Text="delete itrems" style="position: relative" OnClick="btndelete_Click" />
<asp:GridView ID="gridveiw1" runat="server" AutoGenerateColumns="false">

<Columns>

<asp:TemplateField HeaderText="Name1">

<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%#Eval("name")%>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>


<asp:TemplateField HeaderText="Rank1">

<ItemTemplate>
<asp:Label ID="lblrank" runat="server" Text='<%#Eval("rank")%>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="select">

<ItemTemplate>
<asp:CheckBox ID="chkselect" runat="server" />

</ItemTemplate>

</asp:TemplateField>

</Columns>


</asp:GridView>
<asp:Button ID="btndisplay" runat="server" Text="Display the selected" OnClick="btndisplay_Click" />


.cs:

SqlConnection conn;
SqlCommand cmd,cmd2;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
//conn = new SqlConnection("............");
//conn.Open();
}

private void BindData()
{
conn = new SqlConnection(..............);
conn.Open();
//cmd = new SqlCommand("select name,rank,group from student_data ", conn);
SqlDataAdapter da = new SqlDataAdapter("select name,rank from student_data ", conn);
DataSet ds = new DataSet();
da.Fill(ds);
gridveiw1.DataSource = ds;
gridveiw1.DataBind();

}
// this is for just displaying the selected values.......
protected void btndisplay_Click(object sender, EventArgs e)
{
bool result = false;
string name = string.Empty;
string rank = string.Empty;
foreach (GridViewRow row in gridveiw1.Rows)
{
result=(row.FindControl("chkselect") as CheckBox).Checked;
if (result)
{
name = (row.FindControl("lblname") as Label).Text;
rank = (row.FindControl("lblrank") as Label).Text;
Response.Write(name);
Response.Write(rank);
Response.Write("<BR>");
}
}
}

protected void btndelete_Click(object sender, EventArgs e)
{
for (int i = 0; i < gridveiw1.Rows.Count; i++)
{

CheckBox Chk = (CheckBox)gridveiw1.Rows[i].FindControl("chkselect");
if (Chk.Checked)
{
Label lbl1 = (Label)gridveiw1.Rows[i].FindControl("lblname");
Response.Write(i.ToString());
conn = new SqlConnection(.................................);
conn.Open();

cmd2 = new SqlCommand("delete from student_data where name=@name", conn);
cmd2.Parameters.Add("@name", lbl1.Text);
cmd2.ExecuteNonQuery();
conn.Close();

//Write the delete statement here.
//Deleterecords(Convert.ToInt32(lblname.Text));
}
}
BindData();
}
}



Author: chandramohan    30 Jul 2008Member Level: GoldRating:     Points: 1
Dim row As GridViewRow
For Each row In Me.GridView1.Rows

Dim checkbox As System.Web.UI.WebControls.CheckBox
checkbox = row.FindControl("CheckBox1")

If checkbox.Checked = True Then

Dim idTxt As String = ""

idTxt = row.Cells(0).Text

SqlDataSource1.InsertCommand = "insert statement here"

SqlDataSource1.InsertParameters.Add("status", "Present") ---> sample status
SqlDataSource1.Insert()

End If

SqlDataSource1.InsertParameters.Clear()




Author: Mexi Renjith Mamman    30 Jul 2008Member Level: GoldRating:     Points: 4

you can add this code in the "save new record" event.

for (int i = 0; i < gridview1.Items.Count; i++)
{
CheckBox chkChecked = (CheckBox)gridview1.Items[i].FindControl("chkChecked");

if (chkChecked.Checked)
{
//write your code here to save the records
}
}


Author: Vishnu K J    30 Jul 2008Member Level: GoldRating:     Points: 5
Add this code in the save button's click event

CheckBox chk;
foreach (GridViewRow gvRow in GridView1.Rows)
{
if (gvRow.RowType == DataControlRowType.DataRow)
{
chk = (CheckBox)gvRow.FindControl("chkBox");
if (chk.Checked)
{
}
}
}


Post Reply
You must Sign In to post a response.
Next : what is split() function
Previous : retrieve image fro database display it in imagecontrol
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design

conference calls

Contact Us    Privacy Policy    Terms Of Use