Hi smile,
Check this example. Now please take its reference and correct your code.
Namespaces
C#
using System.Data;
using System.Drawing;
using System.Speech.Synthesis;
VB.Net
Imports System.Data
Imports System.Drawing
Imports System.Speech.Synthesis
Code
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BindGrid();
timer1.Start();
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Token"),
new DataColumn("Room"),
new DataColumn("Doctor")
});
dt.Rows.Add(1, "A", "11", "Room-1", "S");
dt.Rows.Add(2, "B", "22", "Room-2", "K");
dt.Rows.Add(3, "C", "33", "Room-3", "Y");
dt.Rows.Add(4, "D", "44", "Room-4", "Z");
dataGridView1.DataSource = dt;
dataGridView1.ReadOnly = true;
dataGridView1.RowsDefaultCellStyle.BackColor = Color.White;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.White;
dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.SteelBlue;
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.White;
dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.AllowUserToResizeColumns = true;
}
static int i = 0;
public void timer1_Tick(object sender, EventArgs e)
{
try
{
if (dataGridView1.Rows.Count - 1 == i)
{
i = 0;
}
if (dataGridView1.Rows[i].Cells[1].Value != null)
{
SpeechSynthesizer speechSynthesizerObj = new SpeechSynthesizer();
string message = "Mr." + dataGridView1.Rows[i].Cells[1].Value + " ";
message += "Token No." + dataGridView1.Rows[i].Cells[2].Value + " ";
message += dataGridView1.Rows[i].Cells[3].Value + " ";
message += "Doctor " + dataGridView1.Rows[i].Cells[3].Value;
speechSynthesizerObj.SpeakAsync(message);
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
dataGridView1.Rows[j].Selected = i == j ? true : false;
}
i++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
VB.Net
Public Partial Class Form1
Public Sub New()
InitializeComponent()
BindGrid()
timer1.Start()
End Sub
Private Sub BindGrid()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Token"), New DataColumn("Room"), New DataColumn("Doctor")})
dt.Rows.Add(1, "A", "11", "Room-1", "S")
dt.Rows.Add(2, "B", "22", "Room-2", "K")
dt.Rows.Add(3, "C", "33", "Room-3", "Y")
dt.Rows.Add(4, "D", "44", "Room-4", "Z")
dataGridView1.DataSource = dt
dataGridView1.ReadOnly = True
dataGridView1.RowsDefaultCellStyle.BackColor = Color.White
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.White
dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.SteelBlue
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.White
dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
dataGridView1.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dataGridView1.AllowUserToResizeColumns = True
End Sub
Shared i As Integer = 0
Public Sub timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
Try
If dataGridView1.Rows.Count - 1 = i Then
i = 0
End If
If dataGridView1.Rows(i).Cells(1).Value IsNot Nothing Then
Dim speechSynthesizerObj As SpeechSynthesizer = New SpeechSynthesizer()
Dim message As String = "Mr." & dataGridView1.Rows(i).Cells(1).Value & " "
message += "Token No." & dataGridView1.Rows(i).Cells(2).Value & " "
message += dataGridView1.Rows(i).Cells(3).Value & " "
message += "Doctor " & dataGridView1.Rows(i).Cells(3).Value
speechSynthesizerObj.SpeakAsync(message)
For j As Integer = 0 To dataGridView1.Rows.Count - 1
dataGridView1.Rows(j).Selected = If(i = j, True, False)
Next
i += 1
End If
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Sub
End Class
Screenshot