将选中的行保存在dataGridview中 [英] saving Checked Rows in dataGridview

查看:88
本文介绍了将选中的行保存在dataGridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Datagridview
并用一栏带有复选框的值填充.
要求是在保存按钮单击时仅保存该行,
已检查.

如果选中了第一行,则只有第一行将被保存在数据库中.
如果选中了所有行,则将保存所有行.

我尝试过此操作,以保存所有行.

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



如何只保存已检查的行".
Assist



how can I save only the Checked Rows.
Assist

推荐答案

尝试
for (int i = dataGridView1.Rows.Count -1; i >= 0 ; i--)
{
    if ((bool)dataGridView1.Rows[i].Cells[0].FormattedValue)
    {
        InsertRow();  //Code to insert row
    }
}


像这样尝试,
try like this,
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

行.Cells(0)是复选框列的索引,请根据您的复选框索引进行更改

row.Cells(0) is the index of the checkbox column, change it as per your checkbox index


代码在C#中,但是我想您会明白的.

the code is in c#, but i assume you''ll understand.

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. 
 }
}


这篇关于将选中的行保存在dataGridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆