Gridview not showing after Gridview RowCommand event? - c#

I have a GridView that shows a list of restaurants and it's data from my database. In the last column I have a ButtonField. When the user clicks on the button, then the RowCommand fires and another GridView appears with the selected restaurant's reviews.
The issue I am having is that while the RowCommand event is fired, the second GridView gvReviews does not show up at all.
I have tried to set the GridView's visible to true but that does not
seem to work.
I have tried to use a TemplateField with a Button instead but that doesn't work either.
I have tried using if (!IsPostback) statement
Here's a snippet of my GridViews:
<asp:GridView ID="gvRestaurants" runat="server" AutoGenerateColumns="false" OnRowCommand="gvRestaurants_RowCommand">
<Columns>
<asp:BoundField DataField="RestaurantID" HeaderText="ID" />
<asp:BoundField DataField="RestName" HeaderText="Restaurant" />
<asp:BoundField DataField="RestAddr" HeaderText="Address" />
<asp:BoundField DataField="RestCity" HeaderText="City" />
<asp:BoundField DataField="RestState" HeaderText="State" />
<asp:BoundField DataField="RestZip" HeaderText="Zip Code" />
<asp:BoundField DataField="CategoryDesc" HeaderText="Category" />
<asp:ButtonField HeaderText="Reviews" CommandName="viewReviews" Text="View" ButtonType="Button" />
</Columns>
</asp:GridView>
<asp:GridView ID="gvReviews" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RestaurantID" HeaderText="ID" />
<asp:BoundField DataField="RestName" HeaderText="Restaurant" />
<asp:BoundField DataField="ReviewDate" HeaderText="Date of Review" />
<asp:BoundField DataField="FoodQuality" HeaderText="Food Quality" />
<asp:BoundField DataField="ServiceRating" HeaderText="Service" />
<asp:BoundField DataField="AtmosphereRating" HeaderText="Atmosphere" />
<asp:BoundField DataField="PriceRating" HeaderText="Price" />
<asp:BoundField DataField="ReviewText" HeaderText="Review" />
</Columns>
</asp:GridView>
Here's a snippet of the aspx.cs
protected void btnSearch_Click(object sender, EventArgs e)
{
gvRestaurants.Visible = true;
gvAllRestaurants.Visible = false;
DataSet ds = p.SearchByCategory(ddCategories.SelectedItem.Value, ddCategories2.SelectedItem.Value);
gvRestaurants.DataSource = ds;
gvRestaurants.DataBind();
}
protected void gvRestaurants_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "viewReviews")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvRestaurants.Rows[index];
int restID = int.Parse(row.Cells[0].Text);
gvReviews.DataSource = p.GetReview(restID);
gvReviews.DataBind();
gvRestaurants.Visible = false;
gvReviews.Visible = true;
}
else
{
string error = "There are no reviews for this restaurant.";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + error + "');", true);
}
Here's a snippet of the methods i used:
public DataSet SearchByCategory(string category1, string category2)
{
DBConnect objDB = new DBConnect();
objCmd.Parameters.Clear();
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.CommandText = "GetRestaurantByCategory";
SqlParameter sqlParameter = new SqlParameter("#theCategory", category1);
SqlParameter sqlParameter2 = new SqlParameter("#theCategory2", category2);
sqlParameter.Direction = ParameterDirection.Input;
sqlParameter.SqlDbType = SqlDbType.VarChar;
sqlParameter.Size = 50;
sqlParameter2.Direction = ParameterDirection.Input;
sqlParameter2.SqlDbType = SqlDbType.VarChar;
sqlParameter2.Size = 50;
objCmd.Parameters.Add(sqlParameter);
objCmd.Parameters.Add(sqlParameter2);
objDB.GetConnection().Open();
DataSet ds = objDB.GetDataSetUsingCmdObj(objCmd);
objDB.CloseConnection();
return ds;
}
public DataSet GetReview(int restaurant)
{
DBConnect objDB = new DBConnect();
objCmd.Parameters.Clear();
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.CommandText = "GetReviewByRestaurantID";
SqlParameter sqlParameter = new SqlParameter("#theRestaurantID", restaurant);
sqlParameter.Direction = ParameterDirection.Input;
sqlParameter.SqlDbType = SqlDbType.Int;
sqlParameter.Size = 4;
objCmd.Parameters.Add(sqlParameter);
objDB.GetConnection().Open();
DataSet ds = objDB.GetDataSetUsingCmdObj(objCmd);
objDB.CloseConnection();
return ds;
}
Here are my stored procedures used:
CREATE PROCEDURE [dbo].GetRestaurantByCategory
#theCategory varchar(50),
#theCategory2 varchar(50)
AS
SELECT rest.RestaurantID, rest.RestName, rest.RestAddr, rest.RestCity, rest.RestState, rest.RestZip, cat.CategoryDesc
FROM Restaurants rest JOIN Categories cat ON rest.CategoryID = cat.CategoryID
WHERE CategoryDesc = #theCategory OR CategoryDesc = #theCategory2
CREATE PROCEDURE [dbo].GetReviewByRestaurantID
#theRestaurantID int
AS
SELECT rest.RestaurantID, rest.RestName, rev.ReviewDate, rev.FoodQuality, rev.ServiceRating, rev.AtmosphereRating, rev.PriceRating, rev.ReviewText
FROM Restaurants rest JOIN Reviews rev ON rest.RestaurantID = rev.RestaurantID
WHERE Rest.RestaurantID = #theRestaurantID

Your GridView doesn't show because you bind it to a DataSet instead of binding it directly to a DataTable within this DataSet.
Try changing this line:
gvReviews.DataSource = p.GetReview(restID);
To this:
gvReviews.DataSource = p.GetReview(restID).Tables[0];

use this query
SELECT rest.RestaurantID, rest.RestName, rev.ReviewDate, rev.FoodQuality, rev.ServiceRating, rev.AtmosphereRating, rev.PriceRating, rev.ReviewText
FROM Restaurants rest JOIN Reviews rev ON rev.RestaurantID = rest.RestaurantID
WHERE Rest.RestaurantID = #theRestaurantID

Related

Get data loaded from sql on Page_Load

I'm noobie to this :)
I'm getting data from SQL Server in the page_load event handler, and that works fine with SqlDataReader.
I also added a DataTable. I want to use the data when selecting a dropdownlist.
This is my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string mainconn1 = ConfigurationManager.ConnectionStrings["SqlConnection1"].ConnectionString;
SqlConnection sqlconn1 = new SqlConnection(mainconn1);
string Sqlquery1 = "SELECT p.[Name], m.machineid,md.MachineNumber, md.HostName FROM [db].[dbo].[Machine] m INNER JOIN MachineDevice md ON md.MachineID = m.MachineID INNER JOIN property p ON m.PropertyID = p.PropertyID WHERE ([status] = '1') AND (md.DateTimeRetired IS NULL) ORDER BY md.MachineNumber";
SqlCommand sqlcomm1 = new SqlCommand(Sqlquery1, sqlconn1);
sqlconn1.Open();
SqlDataReader rd1 = sqlcomm1.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(rd1);
}
}
And this:
protected void Ort_SelectedIndexChanged1(object sender, EventArgs e)
{
if (Ort.SelectedValue == "Stockholm")
{
// while (rd1.read)
// {
// Machine.DataSource = rd1;
// Machine.DataTextField = "MachineNumber";
// Machine.DataValueField = "MachineNumber";
// Machine.DataBind();
// Machine.Items.Insert(0, new ListItem("-Select Machine-", "0"));
// }
}
}
Is it possible to get data when selectindexchanged, or do I need to ask SQL Server again?
Thanks
Sure, say we have a drop down (combo box) of some cities, and when you select the city, we fill out a gird of hotels.
Is it possible to get data when selectindexchanged, or do I need to ask SQL Server again?
you could in some cases "persist" the data, but the cost is just as high as in most cases hitting the database again.
so, yes, it is standard fair to re-pull that data.
example:
this markup:
<h3>Select Hotel city</h3>
<asp:DropDownList ID="DropDownList1" runat="server"
Width="150px" Height="30px"
DataTextField="City"
AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<br />
<br />
<asp:GridView ID="GVHotels" runat="server" CssClass="table table-hover"
DataKeyNames="ID" AutoGenerateColumns="false" Width="40%" OnRowDataBound="GVHotels_RowDataBound" >
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" HeaderStyle-Width="100" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" HeaderStyle-Width="100" />
<asp:BoundField DataField="HotelName" HeaderText="Hotel Name" HeaderStyle-Width="120"/>
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Description" HeaderText="Province" />
<asp:TemplateField>
<ItemTemplate>
<button runat="server" id="cmdEditBooking"
type="button" class="btn myshadow"
onserverclick="cmdEditBooking_ServerClick">
<span class="glyphicon glyphicon-home"></span>View
</button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
So, we have a combo box (dropdown list) of city to select, and then we display hotels from that city.
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadCombo();
}
void LoadCombo()
{
string strSQL = "SELECT City FROM City ORDER BY City";
SqlCommand cmdSQL = new SqlCommand(strSQL);
DropDownList1.DataSource = MyRstP(cmdSQL);
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("Select City", ""));
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex >0)
{
string strSQL =
#"SELECT * FROM tblHotelsA
WHERE City = #City
ORDER BY HotelName";
SqlCommand cmdSQL = new SqlCommand(strSQL);
cmdSQL.Parameters.Add("#City", SqlDbType.NVarChar).Value = DropDownList1.Text;
GVHotels.DataSource = MyRstP(cmdSQL);
GVHotels.DataBind();
}
}
And both of above use this handy helper routine:
DataTable MyRstP(SqlCommand cmdSQL)
{
DataTable rstData = new DataTable();
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (cmdSQL)
{
cmdSQL.Connection = conn;
conn.Open();
rstData.Load(cmdSQL.ExecuteReader());
}
}
return rstData;
}
So, result:

How to set a column in gridview as hyperlink and click on ID data able to point to other to show data details in C# asp.net

I tried many work around but unable to set the column as hyperlink. This is the output i get.
Output that i get
I want to add the hyperlink as below screenshot:
Output with hyperlink
Click on that link able to show the detail as below
This is my code but i unable to add hyperlink:
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat = "server" AutoGenerateColumns="false" OnDataBound="OnDataBound" GridLines="Both">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-Width="150" />
<asp:BoundField DataField="LOT" HeaderText="LOT" ItemStyle-Width="150" />
<asp:BoundField DataField="TYPE" HeaderText="TYPE" ItemStyle-Width="150" />
<asp:BoundField DataField="KEY" HeaderText="KEY" ItemStyle-Width="150" />
<asp:BoundField DataField="VALUE" HeaderText="VALUE" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
Code behind that i have done:
protected void btn_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DateTime StartDate = Convert.ToDateTime(datepickerstart.Text);
string Date = StartDate.ToString("dd-MMM-yyyy");
DateTime EndDate = Convert.ToDateTime(datepicker.Text);
string dt2 = EndDate.ToString("dd-MMM-yyyy");
GridView1.DataSource = GetData("Oracle query");
GridView1.DataBind();
txtDate.Text = "You Selected Start:" + Date;
txtDate1.Text = "You Selected End:" + EndDate;
}
private DataTable GetData(string query)
{
string oradb = "Data Source=(DESCRIPTION =" +
"(ADDRESS = (PROTOCOL=TCP)(HOST = '')(PORT = ''))" +
"(CONNECT_DATA =" +
"(SERVER = DEDICATED)" +
"(SERVICE_NAME = '')" +
")" +
");User ID='';Password='';";
DataTable dt = new DataTable();
OracleConnection conn = new OracleConnection(oradb);
using (OracleConnection con = new OracleConnection(oradb))
{
using (OracleCommand cmd = new OracleCommand(query))
{
using (OracleDataAdapter sda = new OracleDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
protected void OnDataBound(object sender, EventArgs e)
{
for (int rowIndex = GridView1.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = GridView1.Rows[rowIndex];
GridViewRow previousRow = GridView1.Rows[rowIndex + 1];
if (row.Cells[0].Text == previousRow.Cells[0].Text)
{
row.Cells[0].RowSpan = previousRow.Cells[0].RowSpan < 2 ? 2 :
previousRow.Cells[0].RowSpan + 1;
previousRow.Cells[0].Visible = false;
}
}
}
}
Instead of Bound Field you need to use TemplateField > ItemTemplate > LinkButton just i have used below:
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat = "server" AutoGenerateColumns="false" OnDataBound="OnDataBound" GridLines="Both">
<Columns>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="btnIdLink" runat="server" Text='<%# Bind("ID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LOT" HeaderText="LOT" ItemStyle-Width="150" />
<asp:BoundField DataField="TYPE" HeaderText="TYPE" ItemStyle-Width="150" />
<asp:BoundField DataField="KEY" HeaderText="KEY" ItemStyle-Width="150" />
<asp:BoundField DataField="VALUE" HeaderText="VALUE" ItemStyle-Width="150" />
</Columns>
</asp:GridView>

it showing error in GridView that is does not contain a definition for 'gv_RowUpdating no extension method 'gv_RowUpdating'

i have add link button in GridView , but in RowUpdaing method it is showing error , that is, does not contain a definition for 'gvReportCrime_RowUpdating' and no extension method 'gvReportCrime_RowUpdating' accepting a first argument of type does not contain a definition for 'gvReportCrime_RowUpdating' and no extension method 'gvReportCrime_RowUpdating' accepting a first argument of type ... here is my Admin.aspx page , and Admin.aspx.cs page code
<asp:GridView ID="gvReportCrime" runat="server" DataKeyNames="id" OnPageIndexChanging="gvReportCrime_PageIndexChanging" OnRowCancelingEdit="gvReportCrime_RowCancelingEdit" OnRowDeleting="gvReportCrime_RowDeleting" OnRowEditing="gvReportCrime_RowEditing" OnRowUpdating="gvReportCrime_RowUpdating" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="CNIC" HeaderText="CNIC" />
<asp:BoundField DataField="Phone1" HeaderText="Phone1" />
<asp:BoundField DataField="Phone2" HeaderText="Phone2" />
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:BoundField DataField="Gender" HeaderText="Gender" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:BoundField DataField="CrimeType" HeaderText="CrimeType" />
<asp:BoundField DataField="CrimeDetail" HeaderText="CrimeDetail" />
<asp:CommandField ShowEditButton="true" />
<asp:CommandField ShowDeleteButton="true" />
</Columns>
</asp:GridView>
private void BindData()
{
MySqlConnection conn = new MySqlConnection();
conn.Open();
MySqlCommand cmd = new MySqlCommand("Select * from reportCrime", conn);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gvReportCrime.DataSource = ds;
gvReportCrime.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvReportCrime.DataSource = ds;
gvReportCrime.DataBind();
int columncount = gvReportCrime.Rows[0].Cells.Count;
gvReportCrime.Rows[0].Cells.Clear();
gvReportCrime.Rows[0].Cells.Add(new TableCell());
gvReportCrime.Rows[0].Cells[0].ColumnSpan = columncount;
gvReportCrime.Rows[0].Cells[0].Text = "No Records Found";
}
}
protected void gvReportcrime_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
MySqlConnection conn = new MySqlConnection();
String CNIC = Convert.ToString(gvReportCrime.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow) gvReportCrime.Rows[e.RowIndex];
Label lblID = (Label)row.FindControl("lblID");
//TextBox txtname=(TextBox)gr.cell[].control[];
TextBox txtName = (TextBox)row.Cells[0].Controls[0];
TextBox txtPhone1 = (TextBox)row.Cells[1].Controls[0];
TextBox textPhone2 = (TextBox)row.Cells[2].Controls[0];
TextBox textEmail = (TextBox)row.Cells[3].Controls[0];
TextBox textGemder = (TextBox)row.Cells[4].Controls[0];
TextBox textAddress = (TextBox)row.Cells[5].Controls[0];
TextBox textCity = (TextBox)row.Cells[6].Controls[0];
TextBox textCrimeType = (TextBox)row.Cells[7].Controls[0];
TextBox textCrimeDetail = (TextBox)row.Cells[8].Controls[0];
//TextBox textadd = (TextBox)row.FindControl("txtadd");
//TextBox textc = (TextBox)row.FindControl("txtc");
gvReportCrime.EditIndex = -1;
conn.Open();
//SqlCommand cmd = new SqlCommand("SELECT * FROM reportcrime", conn);
MySqlCommand cmd = new MySqlCommand("update reportcrime set Name='" + Name + "',Phone1='" + Phone1 + "',Phone2='" + Phone2 + "',Email='" + Email + "',Gender='" + Gender + "',City='" + City + "',Address='" + Address + "',CrimeType='" + CrimeType + "',CrimeDetail='" + CrimeDetail + "' WHERE CNIC = '" + CNIC + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
BindData();
gvReportCrime.DataBind();
}

How can i transfer my gridview data to another aspx page?

Inward.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="pono" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField SelectText="Update" ShowSelectButton="True" />
<asp:BoundField DataField="pono" HeaderText="pono" ReadOnly="True" SortExpression="pono" />
<asp:BoundField DataField="podate" HeaderText="podate" SortExpression="podate" />
<asp:BoundField DataField="partyname" HeaderText="partyname" SortExpression="partyname" />
<asp:BoundField DataField="flag" HeaderText="flag" SortExpression="flag" />
<asp:BoundField DataField="itemnm" HeaderText="itemnm" SortExpression="itemnm" />
<asp:BoundField DataField="indt" HeaderText="indt" SortExpression="indt" />
<asp:BoundField DataField="qty" HeaderText="qty" SortExpression="qty" />
</Columns>
</asp:GridView>
inward.aspx.cs
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string pono = GridView1.SelectedRow.Cells[0].Text;
Response.Redirect("Update_inward.aspx?pono="+pono);
}
update_inward.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string stcon = ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(stcon);
con.Open();
String query = "Select * from maxus_in where pono=" + Request.QueryString["pono"];
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
TextBox1.Text = ds.Tables[0].Rows[0]["pono"].ToString();
TextBox2.Text = ds.Tables[0].Rows[0]["podate"].ToString();
TextBox3.Text = ds.Tables[0].Rows[0]["partyname"].ToString();
TextBox4.Text = ds.Tables[0].Rows[0]["itemnm"].ToString();
}
con.Close();
}
How do i transfer this data and display in textboxes in another page??
You can Use Query String or store value in the session and retrieve the value on page load of the next page.
username.Value = Request["username"];
username.Value = Session["username"].toString();

GridView in OnRowDeleting no rows are found

I have a couple of GridViews on the page and they both have the problem. When the OnRowDeleting function is fired the GridView (and sender) find no rows. I created the GridView in the following way:
<asp:GridView ID="gridview9" runat="server"
DataKeyNames="id"
OnRowDeleting="deleteNumberFrom9"
AutoGenerateColumns="false"
CssClass="mGrid"
AlternatingRowStyle="alt">
<Columns>
<asp:boundfield datafield="product_key" headertext="PRODUCT KEY" ItemStyle-HorizontalAlign="Center" />
<asp:boundfield datafield="first_name" headertext="FIRST NAME"/>
<asp:boundfield datafield="last_name" headertext="LAST NAME"/>
<asp:boundfield datafield="email" headertext="EMAIL"/>
<asp:boundfield datafield="phone_number" headertext="PHONE NUMBER" ItemStyle-HorizontalAlign="Center" />
<asp:CommandField HeaderText="DELETE NUMBER" ShowDeleteButton="True" ItemStyle-HorizontalAlign="Center" DeleteText="Delete" />
</Columns>
</asp:GridView>
The grid is populated from a MySQL database:
GridView gv = gridview9;
String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName].ToString();
MySqlConnection connection = new MySqlConnection(connectionString);
connection.Open();
MySqlCommand command = new MySqlCommand(queryString, connection);
if (addPhone) command.Parameters.AddWithValue("#phone_number", "%" + phoneNumber + "%");
if (addEmail) command.Parameters.AddWithValue("#email", "%" + email + "%");
if (addFirstName) command.Parameters.AddWithValue("#first_name", "%" + firstName + "%");
if (addLastName) command.Parameters.AddWithValue("#last_name", "%" + lastName + "%");
if (addProductKey) command.Parameters.AddWithValue("#product_key", "%" + productKey + "%");
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
System.Data.DataSet ds = new System.Data.DataSet();
adapter.Fill(ds);
gv.DataSource = ds;
gv.DataBind();
gv.Visible = true;
Finally, the onRowDeleting function is:
protected void deleteNumberFrom9(object sender, GridViewDeleteEventArgs e)
{
int SID = Convert.ToInt32(gridview9.DataKeys[e.RowIndex].Value);
String phone_number = e.Values[4].ToString();
deleteNumber(SID, phone_number, 9);
}
When I run the page, gridview9 properly returns 2 rows but when I click delete in one of the rows and the deleteNumberFrom9 is fired gridview9.Rows.Count is 0 instead of 2. When I add sender to watch list and view the row count it is also 0.
You must rebind the data to the GridView after deleting the record in the database:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void gv_Deleting(object sender, GridViewDeleteEventArgs e)
{
...
deleteNumber(SID, phone_number, 9);
BindData();
}
private void BindData()
{
...
gv.DataSource = ds;
gv.DataBind();
}

Categories

Resources