Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid Checkbox

Status
Not open for further replies.

barrykellett

Programmer
Mar 4, 2003
29
GB
Hi all
I need to create a datagrid with a checkbox in each row which will allow me to choose the clicked row when i press a button on the form.
Pretty much like the address book on yahoo email.

Anyone with any examples?

Ta
BK
 
//when binding the grid, set the DataKeyField
MyDataGrid.DataKeyField = "MyTableID";

...

//elsewhere, in some event handler (e.g. a button click)
//get rows to update (ones with checked values)
foreach( DataGridItem dgi in MyDataGrid.Items )
{
if( ((CheckBox) dgi.Cells[1].Controls[1] ).Checked )
{
int someInt = Convert.ToInt32( MyDataGrid.DataKeys[ dgi.ItemIndex ] );
}
}
 
I actually found a solution to the datagrid of checkboxes where i can choose items and click a button to get the relevant values, but the datagrid will not let me page. The error is:


AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID MyDataGrid when AllowPaging is set to true and the selected datasource does not implement ICollection.


I use the following binding:
Code:
Sub BindData()
        Dim strConn as string = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source = c:\Inetpub\[URL unfurl="true"]wwwroot\aspnet\diving.mdb"[/URL]
        Dim sql as string = "Select * from Contacts"
        Dim conn as New OleDbConnection(strConn)
        Dim objDR as OleDbDataReader
        Dim Cmd as New OleDbCommand(sql, conn)


        conn.Open()
        objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
        MyDataGrid.DataSource = objDR
        MyDataGrid.DataBind()
        conn.close
End Sub

And I have the datagrid to allow paging and page with:

Code:
Sub Page_Change(sender As Object, e As DataGridPageChangedEventArgs)
    dim start as Integer
            start = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize
    MyDataGrid.CurrentPageIndex = e.NewPageIndex
    BindData
End Sub

    Sub PagerButtonClick(sender As Object, e As DataGridPageChangedEventArgs)
        MyDataGrid.CurrentPageIndex = e.NewPageIndex
        BindData
    End Sub


Can anyone please help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top