i need to get index of a value in DataTable.
I’m trying to get like that.
SqlDataAdapter da = new SqlDataAdapter("SELECT MessageID,SenderID,MessageContent FROM Messages WHERE ThreadID="+ThreadID, connectionString);
//Get all messages in the Thread.
DataTable dt = new DataTable();
da.Fill(dt);
da.SelectCommand.CommandText = "SELECT MessageID,SenderID,MessageContent FROM Messages WHERE MessageID="+MessageID;
//Get the message which i need to get index.
DataTable dtMsg = new DataTable();
da.Fill(dtMsg);
//Get index of dtMsg.Rows[0] in dt.
int msgIndex = dt.Rows.IndexOf(dtMsg.Rows[0]);
I analized it when debugging values are same but its returning -1 everytime.
What i can do?
You are looking for a row in datatable dt when the row is actually in datatable dtMsg….
Try:
Actually that is always going to return zero anyway as you are referencing the row by index anyway.
If what you actually want is to find a row in dt based on a value in a dtMsg row you will need to use something like Find() or Select().
Heres’s some sample code:
This assumes that MessageId is the primary index on the table.