I have a form with a GridView on it and it displays the data i want it to perfectly, However I want to bring up an Add/ Edit form using a context menu strip to edit or add a record to the database. To fill the form for the edit part I need to do something like this(actually works when using the standard Data Grid)
Private
Sub
EditToolStripMenuItem_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
EditToolStripMenuItem.Click
Dim
frm
As
New
frmAddEdit
frm.GUID_User =
Me
.dgvUsers.Item(0,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.GUID_Group =
Me
.dgvUsers.Item(1,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtADName.Text =
Me
.dgvUsers.Item(2,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtUserName.Text =
Me
.dgvUsers.Item(3,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtEmail.Text =
Me
.dgvUsers.Item(4,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtLogin.Text =
Me
.dgvUsers.Item(5,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtPassword.Text =
Me
.dgvUsers.Item(6,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtTelNo.Text =
Me
.dgvUsers.Item(7,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtFax.Text =
Me
.dgvUsers.Item(8,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.cbActive.Checked = IIf(
Me
.dgvUsers.Item(9,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString() =
"True"
,
True
,
False
)
frm.GUID_Company =
Me
.dgvUsers.Item(10,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.cbLead.Checked = IIf(
Me
.dgvUsers.Item(11,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString() =
"True"
,
True
,
False
)
frm.cbSplit.Checked = IIf(
Me
.dgvUsers.Item(12,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString() =
"True"
,
True
,
False
)
frm.cbTelesales.Checked = IIf(
Me
.dgvUsers.Item(13,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString() =
"True"
,
True
,
False
)
frm.cboOffice.SelectedValue =
Me
.dgvUsers.Item(14,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtAddress.Text =
Me
.dgvUsers.Item(15,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.Text =
"Edit User"
frm.ShowDialog()
End
Sub
Can Anyone help me with a solution?
What is the Telerik Grid's counterpart to the standard DataGrid's RowIndex
7 Answers, 1 is accepted
Thank you for writing.
In order to get the row index of particular cell you can use its RowInfo property as shown below:
Dim
rowIndex
As
Integer
=
Me
.radGridView1.SelectedCells(0).RowInfo.Index
Hope this helps. Let me know if you have any other questions.
Regards,
Martin Vasilev
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
Thanks for the response, I tried that but I get an ArgumentOutOfRangeException.
Using the code you supplied modified:
Dim
rowIndex
As
Integer
=
Me
.dgvUsers.SelectedCells(0).RowInfo.Index
I get the above mentioned exception, It says the Index was out of range.Must be non-negative and less than the size of the collection.Parameter name: index.
I am very new to programming, As stated before I got this working using the standard visual studio controls.
I also tried dong the following
frm.GUID_User =
Me
.dgvUsers.SelectedCells(0).RowInfo.Index.ToString()
Please Help
Regards
Jesse
Thank you for getting back to me.
I was not able to identify what causes the exception and I will need to check your code to investigate further. It is possible to experience the issue, because the SelectedCells collection is empty (e.g. you do not have selected cells in your grid). I would suggest checking whether the collection is empty, before executing this code. Here is a sample:
If
Me
.radGridView1.SelectedCells.Count > 0
Then
Dim
index
As
Integer
=
Me
.radGridView1.SelectedCells(0).RowInfo.Index
End
If
I hope this helps. If you need further assistance, please do not hesitate to contact me.
All the best,
Martin Vasilev
the Telerik team
Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>
Thanks again for your response, I tried the code you supplied in your last resply and that sorts out the argument out of range exception but i still cant seem to get the form populated the way I need it to be with data from a specific row of the GridView, So that I can edit that specific record which I will then save back to the Database. The Code I used in the first version of the program looked excactly like the code I posted when I started this thread. All I really need is a way to achieve the excact same effect using the Telerik GridView, Ill include all the VB code in a Code Block Below
Public
Class
Users
Private
Sub
Users_Load(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
MyBase
.Load
Fill_Names()
End
Sub
'Fills The Grid
Private
Sub
Fill_Names()
Dim
conn
As
New
System.Data.SqlClient.SqlConnection(DBCon)
Dim
ds
As
New
System.Data.DataSet()
Dim
adapter
As
New
System.Data.SqlClient.SqlDataAdapter()
Dim
strWhere
As
String
=
"SELECT dbo.tbl_USR_Names.GUID_User, dbo.tbl_USR_Names.GUID_Group, dbo.tbl_USR_Names.GUID_Company, dbo.tbl_USR_Names.vch_AD_Name, dbo.tbl_USR_Names.vch_UserName, dbo.tbl_USR_Names.vch_USR_Email, dbo.tbl_USR_Names.vch_Login, dbo.tbl_USR_Names.vch_PWD,dbo.tbl_USR_Names.vch_USR_Tel, dbo.tbl_USR_Names.vch_USR_Fax, dbo.tbl_USR_Names.bit_Active, dbo.tbl_USR_Names.bit_USR_GetLeads,dbo.tbl_USR_Names.bit_USR_GetSplit, dbo.tbl_USR_Names.bit_USR_IsTelesales, dbo.tbl_USR_Names.chr_Office, dbo.tbl_USR_Names.vch_Address,dbo.tbl_SYS_UserGroups.vch_GRP_Description, dbo.tbl_SYS_Company.vch_COM_Name FROM dbo.tbl_USR_Names WITH (nolock) INNER JOIN dbo.tbl_SYS_UserGroups WITH (nolock) ON dbo.tbl_USR_Names.GUID_Group = dbo.tbl_SYS_UserGroups.GUID_GROUP INNER JOIN dbo.tbl_SYS_Company WITH (nolock) ON dbo.tbl_USR_Names.GUID_Company = dbo.tbl_SYS_Company.GUID_Company ORDER BY dbo.tbl_USR_Names.vch_UserName"
adapter.SelectCommand =
New
System.Data.SqlClient.SqlCommand(strWhere, conn)
adapter.SelectCommand.CommandTimeout = 1000
adapter.Fill(ds,
"ProTrack Users"
)
Me
.dgvUsers.SuspendLayout()
Me
.dgvUsers.BestFitColumns()
Me
.dgvUsers.DataSource = ds.Tables(0)
Me
.dgvUsers.Columns(0).IsVisible =
False
Me
.dgvUsers.Columns(1).IsVisible =
False
Me
.dgvUsers.Columns(2).IsVisible =
False
Me
.dgvUsers.ResumeLayout()
conn.Close()
conn.Dispose()
End
Sub
'Brings up a from to add a new user
Private
Sub
AddToolStripMenuItem_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
AddToolStripMenuItem.Click
Dim
frm
As
New
frmAddEdit
frm.Text =
"Add New User"
frm.ShowDialog()
End
Sub
'Edit existing user -- Is Supposed to fill the AddEdit form with data from the grid
Private
Sub
EditToolStripMenuItem_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
EditToolStripMenuItem.Click
Dim
frm
As
New
frmAddEdit
'Solves the ArgumentOutOfRange Exception but the grid is not getting filled.
'If Me.dgvUsers.SelectedCells.Count > 0 Then
' Dim rowIndex As Integer = Me.dgvUsers.SelectedCells(0).RowInfo.Index
'End If
'Tried the Following but it Throws an Argument Out of range exception
'Dim rowIndex As Integer = Me.dgvUsers.SelectedCells(0).RowInfo.Index
'Original code from version one using the standard GataGridView supplied by VS
'as you can see i get the 'RowIndex' not a member of 'Telerik.WinControls.UI.GridViewCellInfo' problem
'I wrote about initially I need to know how to achive the same effect using Telerik controls.
frm.GUID_User =
Me
.dgvUsers.Item(0,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.GUID_Group =
Me
.dgvUsers.Item(1,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtADName.Text =
Me
.dgvUsers.Item(2,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtUserName.Text =
Me
.dgvUsers.Item(3,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtEmail.Text =
Me
.dgvUsers.Item(4,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtLogin.Text =
Me
.dgvUsers.Item(5,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtPassword.Text =
Me
.dgvUsers.Item(6,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtTelNo.Text =
Me
.dgvUsers.Item(7,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtFax.Text =
Me
.dgvUsers.Item(8,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.cbActive.Checked = IIf(
Me
.dgvUsers.Item(9,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString() =
"True"
,
True
,
False
)
frm.GUID_Company =
Me
.dgvUsers.Item(10,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.cbLead.Checked = IIf(
Me
.dgvUsers.Item(11,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString() =
"True"
,
True
,
False
)
frm.cbSplit.Checked = IIf(
Me
.dgvUsers.Item(12,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString() =
"True"
,
True
,
False
)
frm.cbTelesales.Checked = IIf(
Me
.dgvUsers.Item(13,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString() =
"True"
,
True
,
False
)
frm.cboOffice.SelectedValue =
Me
.dgvUsers.Item(14,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.txtAddress.Text =
Me
.dgvUsers.Item(15,
Me
.dgvUsers.SelectedCells(0).RowIndex).Value.ToString()
frm.Text =
"Edit User"
frm.ShowDialog()
End
Sub
End
Class
Public
Class
frmAddEdit
Public
GUID_User
As
String
=
""
Public
GUID_Group
As
String
=
""
Public
GUID_Company
As
String
=
""
Private
Sub
frmAddEdit_Load(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
MyBase
.Load
Fill_Group()
Fill_Company()
End
Sub
Private
Sub
Fill_Group()
Dim
conn
As
New
System.Data.SqlClient.SqlConnection(DBCon)
Dim
ds
As
New
System.Data.DataSet()
Dim
adapter
As
New
System.Data.SqlClient.SqlDataAdapter()
Dim
strWhere
As
String
=
"SELECT GUID_GROUP, vch_GRP_Description FROM [ProTrack].[dbo].[tbl_SYS_UserGroups] WITH (nolock) ORDER BY vch_GRP_Description"
adapter.SelectCommand =
New
System.Data.SqlClient.SqlCommand(strWhere, conn)
adapter.SelectCommand.CommandTimeout = 1000
adapter.Fill(ds,
"Group"
)
Me
.cboGroup.SuspendLayout()
Me
.cboGroup.DataSource = ds.Tables(0)
Me
.cboGroup.DisplayMember =
"vch_GRP_Description"
Me
.cboGroup.ValueMember =
"GUID_GROUP"
Me
.cboGroup.ResumeLayout()
conn.Close()
conn.Dispose()
If
Me
.GUID_Group <>
""
Then
Me
.cboGroup.SelectedValue =
Me
.GUID_Group
End
If
End
Sub
Private
Sub
Fill_Company()
Dim
conn
As
New
System.Data.SqlClient.SqlConnection(DBCon)
Dim
ds
As
New
System.Data.DataSet()
Dim
adapter
As
New
System.Data.SqlClient.SqlDataAdapter()
Dim
strWhere
As
String
=
"SELECT GUID_Company, vch_COM_Name FROM [ProTrack].[dbo].[tbl_SYS_Company] WITH (nolock) ORDER BY vch_COM_Name"
adapter.SelectCommand =
New
System.Data.SqlClient.SqlCommand(strWhere, conn)
adapter.SelectCommand.CommandTimeout = 1000
adapter.Fill(ds,
"Company"
)
Me
.cboCompany.SuspendLayout()
Me
.cboCompany.DataSource = ds.Tables(0)
Me
.cboCompany.DisplayMember =
"vch_COM_Name"
Me
.cboCompany.ValueMember =
"GUID_Company"
Me
.cboCompany.ResumeLayout()
conn.Close()
conn.Dispose()
If
Me
.GUID_Company <>
""
Then
Me
.cboCompany.SelectedValue =
Me
.GUID_Company
End
If
End
Sub
Private
Sub
cmdExit_Click(sender
As
System.
Object
, e
As
System.EventArgs)
Handles
cmdExit.Click
Me
.Close()
End
Sub
Private
Sub
cmdSave_Click(sender
As
System.
Object
, e
As
System.EventArgs)
Handles
cmdSave.Click
Dim
active
As
Integer
= IIf(
Me
.cbActive.Checked, 1, 0)
Dim
split
As
Integer
= IIf(
Me
.cbSplit.Checked, 1, 0)
Dim
lead
As
Integer
= IIf(
Me
.cbLead.Checked, 1, 0)
Dim
telesales
As
Integer
= IIf(
Me
.cbTelesales.Checked, 1, 0)
If
GUID_User =
""
Then
RunSQL(
"INSERT INTO tbl_USR_Names(GUID_User, GUID_Group, vch_AD_Name, vch_UserName, vch_USR_Email, vch_Login, vch_PWD, vch_USR_Tel, vch_USR_Fax, bit_Active, GUID_Company, bit_USR_GetLeads, bit_USR_GetSplit, bit_USR_IsTelesales, chr_Office, vch_Address) VALUES ('"
& Guid.NewGuid.ToString.ToUpper _
&
"', '"
&
Me
.cboGroup.SelectedValue &
"', '"
&
Me
.txtADName.Text &
"', '"
&
Me
.txtUserName.Text &
"', '"
&
Me
.txtEmail.Text &
"', '"
&
Me
.txtLogin.Text &
"', '"
&
Me
.txtPassword.Text &
"', '"
&
Me
.txtTelNo.Text &
"', '"
&
Me
.txtFax.Text & _
"', "
& active &
", '"
&
Me
.cboCompany.SelectedValue &
"',"
& lead &
","
& split &
","
& telesales &
", '"
&
Me
.cboOffice.SelectedValue &
"', '"
&
Me
.txtAddress.Text &
"' )"
)
Else
RunSQL(
"UPDATE tbl_USR_Names SET GUID_Group = '"
&
Me
.cboGroup.SelectedValue &
"', vch_AD_Name = '"
&
Me
.txtADName.Text &
"', vch_UserName = '"
&
Me
.txtUserName.Text &
"', vch_USR_Email = '"
&
Me
.txtEmail.Text &
"', vch_Login= '"
&
Me
.txtLogin.Text &
"', vch_PWD = '"
&
Me
.txtPassword.Text _
&
"', vch_USR_Tel = '"
&
Me
.txtTelNo.Text &
"', vch_USR_Fax = '"
&
Me
.txtFax.Text &
"',bit_Active = "
& active &
" , GUID_Company = '"
&
Me
.cboCompany.SelectedValue &
"',bit_USR_GetLeads = "
& lead &
",bit_USR_GetSplit = "
& split &
" ,bit_USR_IsTelesales = "
& telesales &
", chr_Office = '"
& cboOffice.SelectedValue &
"', vch_Address = '"
&
Me
.txtAddress.Text _
&
"' WHERE GUID_User = '"
&
Me
.GUID_User &
"' "
)
End
If
If
MsgBox(
"Saved!"
, MsgBoxStyle.OkOnly) = MsgBoxResult.Ok
Then
Me
.Close()
End
If
End
Sub
End
Class
Thank you for getting back to me.
Actually, there are several approaches for implementing data editing through a separate form. I suggest you to take a look at the following MSDN article, which gives the general overview of the Winforms data binding.
I have also prepared a simple project, which demonstrates one possible approach. Please refer to the attache file.
I hope you will find it useful. Let me know if you have further questions.
Regards,
Martin Vasilev
the Telerik team
Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>
Once again thank you very much for your help. In the end I found the following solution and it works like a charm.
Private
Sub
EditToolStripMenuItem_Click(sender
As
System.
Object
, e
As
System.EventArgs)
Handles
EditToolStripMenuItem.Click
Dim
frm
As
New
FrmUserAddEdit
Dim
i
As
Integer
= dgvUsers.SelectedRows(0).Index
frm.GUID_User = dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(0).Value.ToString()
frm.GUID_Group = dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(1).Value.ToString()
frm.GUID_Company = dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(2).Value.ToString()
frm.txtADName.Text = dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(3).Value.ToString()
frm.txtUserName.Text = dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(4).Value.ToString()
frm.txtEmail.Text = dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(5).Value.ToString()
frm.txtLogin.Text = dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(6).Value.ToString()
frm.txtPassword.Text = dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(7).Value.ToString()
frm.txtTelNo.Text = dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(8).Value.ToString()
frm.txtFax.Text = dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(9).Value.ToString()
frm.cbActive.Checked = IIf(dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(10).Value.ToString() =
"True"
,
True
,
False
)
frm.cbLead.Checked = IIf(dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(11).Value.ToString() =
"True"
,
True
,
False
)
frm.cbSplit.Checked = IIf(dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(12).Value.ToString() =
"True"
,
True
,
False
)
frm.cbTelesales.Checked = IIf(dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(13).Value.ToString() =
"True"
,
True
,
False
)
frm.cboOffice.SelectedValue = dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(14).Value.ToString()
frm.txtAddress.Text = dgvUsers.Rows(dgvUsers.SelectedRows(0).Index).Cells(15).Value.ToString()
frm.Text =
"Edit User"
frm.ShowDialog()
End
Sub
This gives me the exact result i was lookin for.
Regards
Jesse
I am glad that I could help. Should you have any other questions, do not hesitate to contact us.
Best wishes,
Martin,
the Telerik team
Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>