Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a Datagridview
and is Filled with values with one column having check box.
The requirement is that only that row will be saved on save button click,
which is checked.

If first row is checked then only first row will be saved in database.
and if all rows are checked then all rows will saved.

I have tried this ,to save all the rows.
With MyCommand
For Each row As DataGridViewRow In dgv.Rows

MyParam(0) = CreateParam("@ReturnValue", SqlDbType.Int, ParameterDirection.ReturnValue, 0, 0)
                            MyParam(0) = CreateParam("@TID", SqlDbType.Int, ParameterDirection.Input, 4, lblTID.Text)
                    MyParam(1) = CreateParam("@SlNo", SqlDbType.Int, ParameterDirection.Input, 4, row.Cells(0).Value)
.Parameters.Clear()
                            Dim i As Integer
                            For i = 0 To 1
                                .Parameters.Insert(i, MyParam(i))
                            Next
                            .CommandText = "SPIS_UpdateItemTransDtl"
                            MyConnection.ExecuteCommand(MyCommand)

                        Next
                        lngErrorCode = CInt(.Parameters(0).Value)
                        If lngErrorCode < 0 Then
                            sbErrorMsg(lngErrorCode)
                        Else
                           
                            MsgBox("Record Saved Successfully !")
                        End If

                    End With


how can I save only the Checked Rows.
Assist
Posted
Updated 5-Jul-12 1:05am
v5

Try
C#
for (int i = dataGridView1.Rows.Count -1; i >= 0 ; i--)
{
    if ((bool)dataGridView1.Rows[i].Cells[0].FormattedValue)
    {
        InsertRow();  //Code to insert row
    }
}
 
Share this answer
 
Comments
DamithSL 5-Jul-12 1:54am    
tag as VB.NET
xzmilan 20-Feb-16 14:31pm    
This worked for me. I looked for a solution to my datagridview to perform my SQL to only the selected items. Here is my code..

private void btn_groups_click(object sender, EventArgs e)
{
Bonus.Depts deptfrm = new Depts();
SqlConnection con1 = new SqlConnection();
con1.ConnectionString = @"Data Source=\\SQLEXPRESS;Initial Catalog=...;Integrated Security=True";


string insertcmd = (@" --Truncate Table dbo.deptsel_tbl
Declare @group_ID as int
set @group_ID = (select id from tbl)
INSERT INTO [dbo].[...]
(..,EmployeeDeptCode)
VALUES(.,@EmployeeDeptCode)");

SqlCommand cmd = new SqlCommand(insertcmd, con);


for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
if ((bool)dataGridView1.Rows[i].Cells[0].FormattedValue) //this is where i added this solution
{
cmd.Parameters.Add("@EmployeeDeptCode", dataGridView1.Rows[i].Cells[1].Value);
con.Open();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
con.Close();
}
}
}
hi, try like this,
VB
For Each row As GridViewRow In Me.dgv.Rows
	For Each ctrl As Control In row.Cells(0).Controls
		If ctrl.[GetType]() = GetType(CheckBox) Then					
			If DirectCast(ctrl, Checkbox).Checked Then
                         ' Do insert 
			End If
		End If
	Next
Next
row.Cells(0) is the index of the checkbox column, change it as per your checkbox index
 
Share this answer
 
Comments
Karwa_Vivek 5-Jul-12 7:04am    
its windows application DatagridView ..not in asp.net web.
the code is in c#, but i assume you'll understand.

C#
int row = gvAttendence.Rows.Count;
for (i = 0; i < row; i++)
 {
    if (con.State == ConnectionState.Closed)
       {
           con.Open();
      }
checkbox chk = (checkbox)gridview1.Rows[i].FindControl("your check box id"));
if(chk.checked==true)
 {
   Insert your code to insert that line. 
 }
}
 
Share this answer
 
try this one.

VB
For i As Integer = 0 To Gridview.Rows.Count - 1
	Dim row As GridViewRow = Gridview.Rows(i)
	
	'For Each checked row In Gridview
	Dim isChecked As Boolean = DirectCast(row.FindControl("CheckBox1"), CheckBox).Checked
	If isChecked Then
		InsertRow() 'code to insert row

	End If
Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900