开发者

Index was out of range. datagridview winforms

I think i screwed up something in my code,it throws this exception all the time when the cell clicked

"Index was out of range. Must be non-negative 开发者_StackOverflowand less than the size of the collection. Parameter name: index"

here is my code

[code]
   void dgMessages_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            //try
            //{
                // Ignore clicks that are not on button cells. 
                if (e.RowIndex < 0 || e.ColumnIndex != dgMessages.Columns["clmMessageId"].Index) return;
                //// Retrieve the Row Index
                string RowId = dgMessages[0, e.RowIndex].Value.ToString();

                //// convert string to Integer
                int RowIndex;
                int.TryParse(RowId, out RowIndex);
                //Row Index calculation here
                int RowAdj = RowIndex - 1;
                //Retrive the Error Queue Message for that particular Row Index
                 string MesId = dgMessages.Rows[RowAdj].Cells["clmTransMessage"].Value.ToString();
[/code]

this is where it throw's exception "string MesId = dgMessages.Rows[RowAdj].Cells["clmTransMessage"].Value.ToString();".

Am filling my datagridview from sql table.

this my datagridview binding

 private void tbnFillgrid_Click(object sender, EventArgs e)
        {
            //try
            //{
//               
                 dgMessages.AutoGenerateColumns = false;


                string sql = "select * from tblOriginalmessage om left join dbo.tblTraceMessages tm on om.OriginalMessageId = tm.messageid left join tblTransformedMessage transm on om.OriginalMessageId = transm.fkOriginalMessageID ";
                SqlConnection connection = new SqlConnection(CONNECTION_STRING);
                //SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
                dataadapter = new SqlDataAdapter(sql, connection);
                // DataSet ds = new DataSet();
                ds = new DataSet();
                connection.Open();
                dataadapter.Fill(ds, scrollVal, 5, "tbltraceMessages");
                connection.Close();
                dgMessages.DataSource = ds;
                dgMessages.DataMember = "tblTraceMessages";
                dgMessages.Columns["clmMessageId"].DataPropertyName = "TraceMessageId";
                dgMessages.Columns["clmNo"].DataPropertyName = "ID";
                dgMessages.Columns["clmSender"].DataPropertyName = "Sender";
                dgMessages.Columns["clmType"].DataPropertyName = "Type";
                dgMessages.Columns["clmCreated"].DataPropertyName = "Received";
                dgMessages.Columns["clmSaved"].DataPropertyName = "Transformed";
                dgMessages.Columns["clmTransMessage"].DataPropertyName = "xmlTransformedMessageContent";
                dgMessages.Columns["clmOriginalMessage"].DataPropertyName = "MessageBody";
}

Index was out of range. datagridview winforms


Try this one:

string MesId = dgMessages.Rows[e.RowIndex].Cells["clmTransMessage"].Value.ToString()


if rowindex = 0 (ie first row) you are taking 1 off that and making it -1 there will never be row at -1


If RowIndex is 0, RowAdj will be assigned -1. The call to dgMessages.Rows[-1] is what fails.


Before the assignment, you can validate whether the particular cell has the value or not.

if (dgMessages.Rows[RowAdj].Cells["clmTransMessage"].HasValue)
{
   String MesId=dgMessages.Rows[RowAdj].Cells["clmTransMessage"].Value.ToString();
}

Itz safer code to avoid index out of range error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜