hi...i m again here...
i have a datagridview on a form which contains studentname (in alphabetical order) and a blank rollno field according to the particular class.Now what i m suppose to do is to assign roll no on button click, starting from first student to last student.
Roll no is like 11DCE001...For eg:-
Roll No StudentName
11DCE001 A
11DCE002 B
11DCE003 C
....... .
....... .
til last row....
We can take a textbox to define the starting value..and checkboxes if needed..
please assist me on how to acccomplish this task.

Recommended Answers

All 8 Replies

For i = 0 To DataGridView1.Rows.Count - 1
    DataGridView1.Item(0, i).Value = Mid("11DCE000", 1, 8 - (i + 1).ToString.Length) + (i + 1).ToString
Next

sir,
11DCE0 is not fixed since there so many classes so it keep changing

This shows you the way. You can customize it to suit your needs.

thnxs friend...appreciate ur effort..
again i want to assign enrolment no to students like this 11-0001 to 11-9999.is this possible????

It's the same thing:

For i = 0 To DataGridView1.Rows.Count - 1 
DataGridView1.Item(0, i).Value = Mid("11-0000", 1, 7 - (i + 1).ToString.Length) + (i + 1).ToString
Next

but you might want to check that datagridview.rows.count isn't bigger that 9999 and has at least one record to satisfy your criteria

You can do it this way:

Private Sub button1_Click(sender As Object, e As EventArgs)
	Dim couter As Integer = 1
	For i As Integer = 0 To dataGridView1.Rows.Count - 1
		If Not dataGridView1.Rows(i).IsNewRow Then
			dataGridView1("rollNo", i).Value = [String].Format("{0}-{1}", "11", System.Math.Max(System.Threading.Interlocked.Increment(couter),couter - 1).ToString().PadLeft(4, "0"C))
		End If
	Next
End Sub

The code addes values form "11-0001" to the last student in the dgv. Enumeration is by 1 up.

It really worked.i m very very thankful to all of u helping hands.i m realy glad..thnxs again...
one question again...i have checkbox and other columns in my datagridview..now i want to keep the grid non-editable,so i go through the properties and uncheck 'enable editing'.....but the problem is now i m unable to check the checkbox column in the grid.Hope u got my question!!!!!!!!

For i = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Item(0, i).Value = Mid("11-0000", 1, 7 - (i + 1).ToString.Length) + (i + 1).ToString
Next

it works fine in datagridview but how to insert these roll no to database??????????????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.