Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Am trying to get cell value from a columm and am geting the error below:
MSIL
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


This is my code but am not sure about it and i cant call the column by name it does not allow me to do that.

C#
protected void btnSearchEmployees_Click(object sender, EventArgs e)
   {
       string n = grdAvailableStuff.Rows[2].RowIndex.ToString();
       MessageBox.Show(n);
   }
Posted
Comments
pankajupadhyay29 16-Jun-11 14:23pm    
can u provide markup means html where is this button placed? are u using auto-generated columns??
Anele Ngqandu 16-Jun-11 14:41pm    
My Button

<asp:Button ID="btnSearchEmployees" runat="server" Text="..." Height="24px"
Width="64px" onclick="btnSearchEmployees_Click" />

My Gridview

<asp:GridView ID="grdAvailableStuff" runat="server" BackColor="White"
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4"
EnableSortingAndPagingCallbacks="True" AllowPaging="True"
EditIndex="2" PageSize="5" SelectedIndex="3"
Width="525px" onrowdatabound="grdAvailableStuff_RowCreated"
>
<footerstyle backcolor="#FFFFCC" forecolor="#330099">
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerSettings NextPageText="" PreviousPageText="" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<rowstyle backcolor="White" forecolor="#330099">
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<sortedascendingcellstyle backcolor="#FEFCEB">
<sortedascendingheaderstyle backcolor="#AF0101">
<sorteddescendingcellstyle backcolor="#F6F0C0">
<sorteddescendingheaderstyle backcolor="#7E0000">

<columns>

<asp:HyperLinkField HeaderText="Create Appointment" Text="Create Appointment"
DataNavigateUrlFormatString="FinalizeAppointment.aspx?name={0}&EmployeeID={1}" DataNavigateUrlFields="name,EmployeeID" />



below is the code to load my grid

protected void btnSearch_Click(object sender, EventArgs e)
{

populateDDLStaff();

DataTable dt = new DataTable();
systemBusinessLayer = new BusinessLayer();

dt = systemBusinessLayer.grid(DatePicker.SelectedDate.ToShortDateString());
grdAvailableStuff.DataSource = dt;
grdAvailableStuff.DataBind();



}

Below is my class

public DataTable grid(string date)
{
using (SqlConnection con = new SqlConnection(ConnString))
{
SqlCommand cmd = new SqlCommand("procAvailableStuff", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Date", SqlDbType.NVarChar));
cmd.Parameters["@Date"].Value = date;

DataTable t = new DataTable("tt");
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(t);

return t;


}


}

when your getting the value of a cell in grid , u require to specify cell position
grid.Rows[0].Cells[0].Text.ToString();

that retrieves the cell value of first one in first row
 
Share this answer
 
v2
dataGridView.CurrentRow.Cells["//Column Name here"].Value

It retrieve your cell value.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900