multiple select buttons in gridview - c#

I'm trying to create multiple select buttons in grid view
however I seem to be having some issues
the record is not selected using the code below
<asp:GridView ID="GridView1" runat="server" CssClass="table table-bordered text-nowrap" AutoGenerateColumns="False" DataKeyNames="Qutation_ID" OnRowDeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="Qutation_ID" HeaderText="Qutation ID" InsertVisible="False" ReadOnly="True" SortExpression="Qutation_ID" />
<asp:BoundField DataField="CustID" HeaderStyle-CssClass="hiddencol" HeaderText="Customer ID" InsertVisible="False" ItemStyle-CssClass="hiddencol" ReadOnly="True" SortExpression="Qutation_ID">
<HeaderStyle CssClass="hiddencol" />
<ItemStyle CssClass="hiddencol" />
</asp:BoundField>
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<asp:BoundField DataField="Subject" HeaderText="Subject" SortExpression="Subject" />
<asp:ButtonField Text="Click" CommandName="ActionCommand" ItemStyle-Width="30" />
</Columns>
<EmptyDataTemplate>
"No records found"
</EmptyDataTemplate>
</asp:GridView>
protected void ActionCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ActionCommand")
{
msg.Text = GridView1.SelectedRow.Cells[1].Text;
}
}

Related

how to get cell value from gridview

protected void CovidSelfDecleartionDeatils_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
ShowallRecord();
if (e.CommandArgument != "")
{
int Index = Convert.ToInt32(e.CommandArgument);
if (e.CommandName=="Select")
{
DataTable dt_main = new DataTable();
dt_main = (DataTable)ViewState["ReportData"];
GridViewRow clickedRow = CovidSelfDecleartionDeatils.Rows[Index];
Session["Filepath"] = clickedRow.Cells[9].Text;
//Session["Filepath"] = CovidSelfDecleartionDeatils.Rows[Index].FindControl("FilePath");
Session["Filepath1"] = CovidSelfDecleartionDeatils.DataKeys[Index]["Filepath"].ToString();
string filepath = Session["Filepath"].ToString();
//DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows;
//string val = (string)rows[2].Cells["Late_Time"].Value;
}
}
}
catch
{
}
}
<asp:GridView ID="CovidSelfDecleartionDeatils" runat="server" DataKeyNames="EmployeeCode" AutoGenerateColumns="False"
BackColor="#333333" CellPadding="5" CellSpacing="3" ForeColor="Black" Width="100%" Height="300px" OnRowCommand="CovidSelfDecleartionDeatils_RowCommand" >
<Columns>
<%-- <asp:BoundField DataField="InitiateMPPId" HeaderText="InitiateMPPId" >
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>--%>
<asp:BoundField DataField="EmployeeCode" HeaderText="EmployeeCode" ItemStyle-HorizontalAlign="Left" Visible="false"
HeaderStyle-HorizontalAlign="Left" ControlStyle-BorderStyle="None" ItemStyle-CssClass="per10"
HeaderStyle-CssClass="header-center">
</asp:BoundField>
<%-- <asp:BoundField DataField="EmployeeCode" HeaderText="EmployeeCode" ItemStyle-HorizontalAlign="Left" Visible="false"
HeaderStyle-HorizontalAlign="Left" ControlStyle-BorderStyle="None" ItemStyle-CssClass="per10"
HeaderStyle-CssClass="header-center">
</asp:BoundField>--%>
<asp:BoundField DataField="EmpName" HeaderText="Employee" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" >
</asp:BoundField>
<asp:BoundField DataField="Decleartion_Date" HeaderText="Date Decleartion" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" DataFormatString="{0:dd-MM-yyyy}" >
</asp:BoundField>
<asp:BoundField DataField="Ferver" HeaderText="Ferver" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="Cough" HeaderText="Cough" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left"/>
<asp:BoundField DataField="Breathlessness" HeaderText="Breathlessness" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left"/>
<asp:BoundField DataField="OtherDiseases" HeaderText="OtherDiseases" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-CssClass="per50" ItemStyle-Height="25" HeaderStyle-Height="25" HeaderStyle-CssClass="header-center" HeaderStyle-Wrap="true"/>
<asp:BoundField DataField="FileName" HeaderText="FileName" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-CssClass="per50" ItemStyle-Height="25" HeaderStyle-Height="25" HeaderStyle-CssClass="header-center" HeaderStyle-Wrap="true" Visible="false" />
<asp:BoundField DataField="DeclarationID" HeaderText="DeclarationID" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-CssClass="per50" ItemStyle-Height="25" HeaderStyle-Height="25" HeaderStyle-CssClass="header-center" HeaderStyle-Wrap="true" Visible="false" />
<asp:BoundField DataField="FilePath" HeaderText="FilePathfordownload" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-CssClass="per50" ItemStyle-Height="25" HeaderStyle-Height="25" HeaderStyle-CssClass="header-center" HeaderStyle-Wrap="true" Visible="false" />
<asp:BoundField DataField="ContactwithCovid" HeaderText="Incontact with covid patient Last(14) days" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-CssClass="per50" ItemStyle-Height="25" HeaderStyle-Height="25" HeaderStyle-Width="15%" HeaderStyle-CssClass="header-center" HeaderStyle-Wrap="true"/>
<%--<asp:TemplateField HeaderText="Status" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="btn_delete_record"
Width="20px" Height="20px" runat="server" ImageUrl="~/Content/Images/NewIcons/PDF.png" />
</ItemTemplate>
</asp:TemplateField>--%>
<asp:ButtonField CommandName="Select" ControlStyle-CssClass="button" ButtonType="Image" ImageUrl="~/Content/Images/NewIcons/PDF.png"
ItemStyle-Width="5px" HeaderText="Download Attachment" />
<asp:BoundField DataField="City" HeaderText="City" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="TravelDate" HeaderText="TravelDate" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" DataFormatString="{0:dd-MM-yyyy}" />
<asp:BoundField DataField="StateName" HeaderText="StateName" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="#5e5d5d" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="Gray" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
what wrong in my showing excetion " Object reference is not set" when i am saving value in session
remove visible=false and define new css class like
.donotshow { display: none; }
and add it HeaderStyle-CssClass and ItemStyle-CssClass

System.ArgumentOutOfRangeException on my RowDataBound

This code return the error "System.ArgumentOutOfRangeException". I know this error but my gridView have 13 columns and I read the column 2 and 4 so I don't understand...
Only e.Row.Cells[0] works so I can't read the other columns.
I looked on the forum but I can not find my problem. Often this problem is due to the fact that the person tries to read a column that does not exist, this is not my case.
protected void grd_accident_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Check CM
if (e.Row.Cells[1].Text.Equals("false"))
{
e.Row.Cells[1].Text = "<span class='glyphicon glyphicon-remove' aria-hidden='true'></span>";
}
else
{
e.Row.Cells[1].Text = "<span class='glyphicon glyphicon-ok' aria-hidden='true'></span>";
}
////Check HSE
if (e.Row.Cells[3].Text.Equals("2"))
{
e.Row.Cells[3].Text = "<span class='glyphicon glyphicon-remove' aria-hidden='true'></span>";
}
else
{
e.Row.Cells[3].Text = "<span class='glyphicon glyphicon-ok' aria-hidden='true'></span>";
}
}
If it can help, my gridView :
<asp:GridView ID="grd_accident" runat="server" CellPadding="4" AutoGenerateColumns="False"
AllowPaging="True"
CssClass="mGrid"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" DataSourceID="ATP_rechercheRes" OnRowDataBound="grd_accident_RowDataBound" OnRowCommand="grd_accident_RowCommand">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:BoundField DataField="columna" HeaderText="Type" SortExpression="columna" />
<asp:BoundField DataField="columnb" HeaderText="ID" ReadOnly="True" SortExpression="columnb" />
<asp:BoundField DataField="columnc" ItemStyle-Width="90px" HeaderText="Date" SortExpression="columnc" />
<asp:BoundField DataField="columnd" HeaderText="Heure" SortExpression="columnd" />
<asp:BoundField DataField="columne" HeaderText="Nom" SortExpression="columne" />
<asp:BoundField DataField="columnf" HeaderText="Prénom" SortExpression="columnf" />
<asp:BoundField DataField="columng" HeaderText="Badge" SortExpression="columng" />
<asp:BoundField DataField="columnh" HeaderText="Société" SortExpression="columnh" />
<asp:BoundField DataField="columni" HeaderText="Description" SortExpression="columni" />
<asp:BoundField DataField="columnj" ItemStyle-HorizontalAlign="Center" HeaderText="Code EPI" SortExpression="columnj" />
<asp:BoundField DataField="columnk" ItemStyle-HorizontalAlign="Center" HeaderText="Advisor" SortExpression="columnk" />
<asp:BoundField DataField="columnl" ItemStyle-HorizontalAlign="Center" HeaderText="HSE" SortExpression="columnl" />
<asp:TemplateField ItemStyle-Width="130px">
<ItemTemplate>
<asp:ImageButton ID="btn_goToAccidentInfirmiere" ImageUrl="images/infirmerie.png" Width="37px" runat="server"
CommandName="goToAccidentInfirmiere"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Infirmière ►" />
<asp:ImageButton ID="btn_goToAccidentCm" ImageUrl="images/factory.png" Width="37px" runat="server"
CommandName="goToAccidentCm"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="CM ►" />
<asp:ImageButton ID="btn_genRapport" ImageUrl="images/rapport.png" Width="37px" runat="server" CommandName="genRap"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="pgr"></PagerStyle>
</asp:GridView>
I found the solution, I removed AllowPaging="True" on from the properties of GridView. It works now

Make asp buttonfield visible at runtime

I have an <asp:ButtonField> inside a gridview. How do I target the button in the gridview to make it Visible on runtime based on a condition ? I am not able to target it since it doesnt have the ID property. I am stuck here. Here is the code below
<asp:GridView ID="OrdersDataList1" runat="server" DataKeyNames="OrderID" Width="100%" SkinID="Gridview" OnPageIndexChanging="orders_PageIndexChanging"
EmptyDataText="You have no orders." AllowSorting="True" OnSorting="OnSort" AllowPaging="true" PageSize="15" AutoGenerateColumns="False" OnRowCommand="updateStatus">
<Columns>
<asp:BoundField DataField="CustomerUser_ID" HeaderText="UserID" Visible="true" />
<asp:BoundField DataField="OrderID" HeaderText="OrderNo" InsertVisible="False" ReadOnly="True" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" SortExpression="OrderID" />
<asp:BoundField DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"/>
<asp:BoundField DataField="Base" HeaderText="Base" DataFormatString="{0:C}" SortExpression="Base" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"/>
<asp:BoundField DataField="Freight" HeaderText="Freight" DataFormatString="{0:C}" SortExpression="Freight" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"/>
<asp:BoundField DataField="Total" HeaderText="Total" DataFormatString="{0:C}" SortExpression="Total" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"/>
<asp:BoundField DataField="Products" HeaderText="Products" SortExpression="Products" DataFormatString="{0} product" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"/>
<asp:BoundField DataField="Units" HeaderText="Units" SortExpression="Units" DataFormatString="{0} units" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"/>
<asp:BoundField DataField="OrderStatusName" HeaderText="Current Status" SortExpression="OrderStatusName" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"/>
<asp:BoundField DataField="OrderStatusID" HeaderText="Orderstatusid" Visible="true" />
<asp:BoundField DataField="OrderTracking_ID" HeaderText="TrackingNo" SortExpression="OrderTracking_ID" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"/>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<%# GetViewOrderLink(Eval("OrderID").ToString(), Eval("OrderState").ToString())%>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Change Status" SortExpression="OrderStatusName">
<ItemTemplate>
<asp:DropDownList ID="OrderStatusDD" runat="server"
DataSourceID="OrdersStatuses" DataTextField="OrderStatusName" DataValueField="OrderStatusID" Visible="false">
</asp:DropDownList>
<asp:SqlDataSource ID="OrdersStatuses" runat="server"
ConnectionString="<%$ ConnectionStrings:SqlConn %>"
SelectCommand="SELECT [OrderStatusID], [OrderStatusName] FROM [Orders_Statuses] where OrderStatusID = 2 or OrderStatusID = 8 ORDER BY [OrderStatusName]">
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" Text="Update status" Visible="false" HeaderText="Change Status" />
</Columns>
</asp:GridView>
PS I have the onrowCommand set on the gridview to listen to button click in gridview
You need to create RowCreated event for the gridview example
Before the GridView control can be rendered, a GridViewRow object must be created for each row in the control. The RowCreated event is raised when each row in the GridView control is created. This enables you to provide an event-handling method that performs a custom routine, such as adding custom content to a row, whenever this event occurs.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
}
OR
<asp:GridView runat="server" ID="GV1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Age" HeaderText="Age" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="Reject"
Visible='<%# IsOverAgeLimit((Decimal)Eval("Age")) %>'
CommandName="Select"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected Boolean IsOverAgeLimit(Decimal Age) {
return Age > 35M;
}
Reference example
You can use the RowCreated event in the GridView as follows:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
Button btn = (Button) e.Row.Cells[12].Controls[0];
if(1==1)
{
btn.Visible = true;
}
}
((BoundField)grv_selec.Columns[1]).DataFormatString = "{0:N2}";

Deleting in gridview not working

i designed a page for admin to search users from database table using a Get button and delete users in gridview
This my source coding..
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" DataKeyNames="username" EnableModelValidation="True" OnRowDeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="adm" HeaderText="adm" SortExpression="adm" />
<asp:BoundField DataField="mobno" HeaderText="mobno" SortExpression="mobno" />
<asp:BoundField DataField="branch" HeaderText="branch" SortExpression="branch" />
<asp:BoundField DataField="year" HeaderText="year" SortExpression="year" />
<asp:BoundField DataField="username" HeaderText="username" ReadOnly="True" SortExpression="username" />
<asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
<asp:BoundField DataField="usertype" HeaderText="usertype" SortExpression="usertype" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CanteenConnectionString %>" SelectCommand="SELECT * FROM [Hosteller]" DeleteCommand="DELETE FROM Hosteller WHERE (username = #username)">
<DeleteParameters>
<asp:Parameter Name="username" />
</DeleteParameters>
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</form>
protected void btnget_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string ConString = "Data Source=sheikha-pc\\sqlexpress;Initial Catalog=Canteen;Integrated Security=True";
SqlConnection con = new SqlConnection(ConString);
string qry = "select id,name,adm,mobno,branch,year,username,password,usertype from Hosteller where adm='" + txtsearch1.Text + "'";
SqlDataAdapter adpt = new SqlDataAdapter(qry, con);
adpt.Fill(dt);
if (dt.Rows.Count == 0)
{
GridView1.EmptyDataText = "No data found";
GridView1.DataBind();
}
else
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
this is the coding that i wrote for GET button
the problem is when i click on delete it doesnt work! please help.........
It looks like the issue here is that you are overriding the data source in your code behind. If you comment out the code in "btnget_Click" and add the datasource to GridView1 it should work to delete.
<asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" DataKeyNames="username" EnableModelValidation="True" OnRowDeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="adm" HeaderText="adm" SortExpression="adm" />
<asp:BoundField DataField="mobno" HeaderText="mobno" SortExpression="mobno" />
<asp:BoundField DataField="branch" HeaderText="branch" SortExpression="branch" />
<asp:BoundField DataField="year" HeaderText="year" SortExpression="year" />
<asp:BoundField DataField="username" HeaderText="username" ReadOnly="True" SortExpression="username" />
<asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
<asp:BoundField DataField="usertype" HeaderText="usertype" SortExpression="usertype" />
</Columns>
</asp:GridView>
And the code behind:
protected void btnget_Click(object sender, EventArgs e)
{
}
This might not be what you want though. This will load the data on page load so the user will not need to click the button in order to get the results.
If you want the user to click the button then you will need to rewirte your code a bit and put some code in RowDeleting() as #Cal279 sugested in his comment.

Delete row using gridview_rowdeleting event

I'm trying to delete a row in Grid View programmatically
I have created this GridView
<asp:GridView ID="GridView1" CssClass="HeaderTables" runat="server" AllowPaging="True"
EmptyDataText="There is no data record to display"
AllowSorting="True" AutoGenerateColumns="false"
CellPadding="0" Height="0px" Width="800px"
onpageindexchanging="GridView1_PageIndexChanging"
onsorting="GridView1_Sorting" onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="first_name" HeaderText="First name"/>
<asp:BoundField DataField="last_name" HeaderText="Last name"/>
<asp:BoundField DataField="mobile_phone" HeaderText="Mobile number"/>
<asp:BoundField DataField="email" HeaderText="Email"/>
<asp:BoundField DataField="city" HeaderText="City"/>
<asp:BoundField DataField="street_number" HeaderText="Street number"/>
<asp:CommandField ShowEditButton="True" ButtonType="Button" />
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
</Columns>
<HeaderStyle HorizontalAlign="Left" />
<RowStyle HorizontalAlign="Left" />
</asp:GridView>
and my code behind is:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (MessageBox.Show("Are you sure you want to delete this data?",
"Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["mySqlString"].ConnectionString);
MySqlCommand cmd = new MySqlCommand("DELETE FROM persons WHERE id = #id", conn);
MySqlParameter param = new MySqlParameter();
try
{
int rowID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
cmd.Parameters.AddWithValue("#id", rowID);
conn.Open();
cmd.ExecuteNonQuery();
GridView1.DataBind();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
}
I'm getting the error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Your grid doesn't set the DataKeyNames property, so this grid isn't tracking any datakeys. Probably thats why you are getting an index error.
You should set the DataKeyNames property. In your code you also need to check to make sure the collection contains elements.The datakeys collection itself may not be null, but it can contain zero elements.
Use DataKeyNames="ID" like
<asp:GridView ID="GridView1" CssClass="HeaderTables" runat="server" AllowPaging="True"
EmptyDataText="There is no data record to display" DataKeyNames="ID"
AllowSorting="True" AutoGenerateColumns="false"
CellPadding="0" Height="0px" Width="800px"
onpageindexchanging="GridView1_PageIndexChanging"
onsorting="GridView1_Sorting" onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="first_name" HeaderText="First name"/>
<asp:BoundField DataField="last_name" HeaderText="Last name"/>
<asp:BoundField DataField="mobile_phone" HeaderText="Mobile number"/>
<asp:BoundField DataField="email" HeaderText="Email"/>
<asp:BoundField DataField="city" HeaderText="City"/>
<asp:BoundField DataField="street_number" HeaderText="Street number"/>
<asp:CommandField ShowEditButton="True" ButtonType="Button" />
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
</Columns>
<HeaderStyle HorizontalAlign="Left" />
<RowStyle HorizontalAlign="Left" />
</asp:GridView>
Then it will work for you
Your forgot to add DataKeyNames to your gridview
DataKeyNames="Valid Column Name" //Column name here instead of Valid Column name
Full Code below :
<asp:GridView ID="GridView1" DataKeyNames="Valid Column Name" CssClass="HeaderTables" runat="server" AllowPaging="True"
EmptyDataText="There is no data record to display"
AllowSorting="True" AutoGenerateColumns="false"
CellPadding="0" Height="0px" Width="800px"
onpageindexchanging="GridView1_PageIndexChanging"
onsorting="GridView1_Sorting" onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="first_name" HeaderText="First name"/>
<asp:BoundField DataField="last_name" HeaderText="Last name"/>
<asp:BoundField DataField="mobile_phone" HeaderText="Mobile number"/>
<asp:BoundField DataField="email" HeaderText="Email"/>
<asp:BoundField DataField="city" HeaderText="City"/>
<asp:BoundField DataField="street_number" HeaderText="Street number"/>
<asp:CommandField ShowEditButton="True" ButtonType="Button" />
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
</Columns>
<HeaderStyle HorizontalAlign="Left" />
<RowStyle HorizontalAlign="Left" />
</asp:GridView>

Categories