I have a Gridview that allows user to edit and delete the records. I am using .net 3.5 using C#.
When I have just one record my edit command is working fine but problem comes when there are more that 1 record in my gridview.
In this case while binding the controls it it is unable to find the controls the second time and thus throws the error.
Here is my code:
protected void gdScheduleNew_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (ViewState["Updating"].Equals(true))
{
TextBox txtactsta = (TextBox)e.Row.FindControl("txtactualstartDate");
TextBox txtactend = (TextBox)e.Row.FindControl("txtactualendDate");
LinkButton lnkUpdate = (LinkButton)e.Row.FindControl("lnkUpdate");
txtactsta.Attributes.Add("ReadOnly", "true");
txtactend.Attributes.Add("ReadOnly", "true");
}
}
}
markup code is:
<asp:GridView ID="gdScheduleNew" runat="server" Width="100%" SelectedIndex="0"
CellPadding="4" AutoGenerateColumns="False" ForeColor="#333333" Height="100px"
OnRowDataBound="gdScheduleNew_RowDataBound" onrowediting="gdSchedule_RowEditing">
<FooterStyle Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="Planned Efforts">
<ItemTemplate>
<asp:Label ID="lblscheduleid" runat="server" Text='<%# Bind("scheduleid") %>'
Visible="false"></asp:Label>
<asp:Label ID="lblPlannedEffort" runat="server" Text='<%# Bind("sepcet") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblscheduleid" runat="server" Text='<%# Bind("scheduleid") %>'
Visible="false"></asp:Label>
<asp:TextBox ID="txtsepcet" runat="server" Text='<%# Bind("sepcet") %>'
Width="88px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtsepcet" runat="server" Width="88px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actual Efforts">
<ItemTemplate>
<asp:Label ID="lblActualEffort" runat="server"
Text='<%# Bind("actualeffort") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtactualeffort" runat="server"
Text='<%# Bind("actualeffort") %>' Width="88px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtactualeffort" runat="server" Width="88px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Planned Start Date">
<ItemTemplate>
<asp:Label ID="lblPlannedStart" runat="server"
Text='<%# Bind("plannedstartDate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtplannedstartDate" runat="server"
Text='<%# Bind("plannedstartDate") %>' class="dateclass" Width="88px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtplannedstartDate" runat="server" class="dateclass"
Width="88px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Planned End Date">
<ItemTemplate>
<asp:Label ID="lblPlannedEnd0" runat="server"
Text='<%# Bind("plannedendDate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtplannedendDate" runat="server"
Text='<%# Bind("plannedendDate") %>' class="dateclass" Width="88px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtplannedendDate" runat="server" class="dateclass"
Width="88px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actual Start Date">
<ItemTemplate>
<asp:Label ID="lblActualStart" runat="server"
Text='<%# Bind("actualstartDate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtactualstartDate" runat="server"
Text='<%# Bind("actualstartDate") %>' class="dateclass" Width="88px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtactualstartDate" runat="server" class="dateclass"
Width="88px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actual End Date">
<ItemTemplate>
<asp:Label ID="lblActualEnd" runat="server"
Text='<%# Bind("actualendDate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtactualendDate" runat="server"
Text='<%# Bind("actualendDate") %>' class="dateclass" Width="88px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtactualendDate" runat="server" class="dateclass"
Width="88px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Member Name">
<ItemTemplate>
<asp:Label ID="lblMember" runat="server" Text='<%# Bind("membername") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlmem" runat="server" DataTextField="MemberName" DataValueField="MemberID">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="fddlmem" runat="server" DataTextField="MemberName" DataValueField="MemberID">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%# Bind("status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<br />
<asp:LinkButton ID="LnkCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkAddnew" runat="server" CausesValidation="true"
CommandName="AddNew" Text="Submit"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The error is on this line: txtactsta.Attributes.Add("ReadOnly", "true");:
object reference no set to an instance of object
How can I fix this?
Doesn't gdScheduleNew_RowDataBound get called for every row in your gridview? txtactualstartDate and txtactualendDate only exist on edit rows, they do not exist on non editing rows.
Try this:
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit)
Related
I have a nested grid views:
<asp:GridView ID="gvRequisitions" runat="server" AutoGenerateColumns="false"
DataKeyNames="PurchaseRequisitionID" OnRowDataBound="OnRowDataBound" ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt="" style="cursor:pointer" src="../images/plus.png" width="12" height="12" />
<asp:Panel ID="pnlItems" runat="server" Style="display: none">
<asp:GridView ID="gvItems" runat="server" AutoGenerateColumns="false" ShowFooter="true"
OnRowDataBound="gvItems_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblItemID" runat="server" Text='<%# Eval("ItemID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddItem" runat="server" OnClick="addItem" Text="add" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Code">
<ItemTemplate>
<asp:Label ID="lblItemCode" runat="server" Text='<%# Eval("ItemCode") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtItemCode" runat="server" Visible="true" Width="100"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>'
Width="225"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDescription" runat="server" Width="225"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date req.">
<ItemTemplate>
<asp:Label ID="lblDateRequired" runat="server" Text='<%# Eval("DateRequired") %>'
Width="75"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDateRequired" runat="server" Width="75"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Measure">
<ItemTemplate>
<asp:Label ID="lblMeasure" runat="server" Text='<%# Eval("MeasureID") %>'
Width="120" ></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlMeasure" runat="server" Width="120"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:Label ID="lblQuantity" runat="server" Text='<%# Eval("Quantity") %>' Width="75"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Width="75"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cost">
<ItemTemplate>
<asp:Label ID="lblCostPerUnit" runat="server" Text='<%# Eval("CostPerUnit") %>' Width="75"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCostPerUnit" runat="server" Width="75"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text='<%# Eval("Total") %>' Width="120"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>No Items For this Requisition</EmptyDataTemplate>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblPurchaseRequisitionID" runat="server" Text='<%# Eval("PurchaseRequisitionID") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddReq" runat="server" Text="Add" OnClick="addPurreq" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Requested By">
<ItemTemplate>
<asp:Label ID="lblRequestor" runat="server" Text='<%# Eval("Requestor") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlRequestor" runat="server" Width="200"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department">
<ItemTemplate>
<asp:Label ID="lblDepartmentID" runat="server" Text='<%# Eval("DepartmentID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlDepartment" runat="server" Width="200" ></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Budget">
<ItemTemplate>
<asp:Label ID="lblBudgetID" runat="server" Text='<%# Eval("BudgetID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlBudget" runat="server" Width="200"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Supplier">
<ItemTemplate>
<asp:Label ID="lblSupplierID" runat="server" Text='<%# Eval("SupplierID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSupplier" runat="server" Width="200"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Created">
<ItemTemplate>
<asp:Label ID="lblDateCreated" runat="server" Text='<%# Eval("DateCreated","{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In code behind I have:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
string purchaseRequisitionID = gvRequisitions.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvItems = (GridView)e.Row.FindControl("gvItems");
// new code to send reference to gvItems_RowDataBound
gvItems.RowDataBound += new GridViewRowEventHandler(gvItems_RowDataBound);
int id = Convert.ToInt32(purchaseRequisitionID);
using (SqlConnection conn = new SqlConnection(constr))
{
using(SqlCommand comm = new SqlCommand("procItems_CRUD"))
{
comm.Parameters.AddWithValue("#Action", "SELECT");
comm.Parameters.AddWithValue("#ID", id);
using(SqlDataAdapter sda = new SqlDataAdapter())
{
comm.CommandType = CommandType.StoredProcedure;
comm.Connection = conn;
sda.SelectCommand = comm;
using(DataTable dt = new DataTable())
{
sda.Fill(dt);
gvItems.DataSource = dt;
gvItems.DataBind();
}
}
}
}
}
}
And:
protected void gvItems_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlMeasure = e.Row.FindControl("ddlMeasure") as DropDownList;
if(ddlMeasure == null)
{
}
}
}
But I cannot see the drop down list as it returns a null value (as I know it is not a DataRow) but how can I see the footer row and the elements in footer row so that I can add another Item to the order/populate DropDownList etc.
Have been trying to sort this for some time and cannot find a working solution so any pointer(s) will be very much appreciated.
You need to use DataControlRowType.Footer.
protected void gvItems_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a footer row
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddlMeasure = e.Row.FindControl("ddlMeasure") as DropDownList;
if (ddlMeasure != null)
{
ddlMeasure.Items.Insert(0, new ListItem("DLL Found!", "-1"));
}
}
}
how am i be able to hide those duplicate columns using gridview command?
im using
nested gridview and left Outer Join on sqlcommand i joined two database.. so yeah this is my next problem
this is my code
protected void NewConnection()
{
SqlCommand cmd = new SqlCommand("SELECT a.*,b.ID,b.Firstname,b.Middlename,b.Lastname,b.Friendswith,b.Amount from [User_TBL_DB] a LEFT OUTER JOIN [Friends_TBL_DB] b ON a.ID=b.Friendswith", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
GroupGridView(GridView1.Rows, 0,0);
GridView1.Columns[3].Visible = false;
GridView1.Columns[4].Visible = false;
GridView1.Columns[7].Visible = false;
GridView1.Columns[8].Visible = false;
GridView1.Columns[9].Visible = false;
GridView1.Columns[10].Visible = false;
GridView1.Columns[11].Visible = false;
foreach (GridViewRow Separate in GridView1.Rows)
{
GridView gv2 = ((GridView)Separate.FindControl("GridView2"));
gv2.DataSource = ds;
gv2.DataBind();
string strfirstname = ((Label)Separate.FindControl("lblfirstname")).Text;
string strmiddlename=((Label)Separate.FindControl("lblmiddlename")).Text;
string strlastname = ((Label)Separate.FindControl("lbllastname")).Text;
string strage = ((Label)Separate.FindControl("lblage")).Text;
string strsex = ((Label)Separate.FindControl("lblsex")).Text;
string stramount = ((Label)Separate.FindControl("lblamount")).Text;
string strID1 = ((Label)Separate.FindControl("lblid")).Text;
foreach (GridViewRow grouplist in gv2.Rows)
{
string strID2 = ((Label)grouplist.FindControl("lblfriendswith2")).Text;
if (strID1 == strID2)
{
grouplist.Visible = true;
}
else
{
grouplist.Visible = false;
}
}
}
}
and this is my asp for gridview1 and gridview2
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Firstname" SortExpression="Firstname">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Firstname") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblfirstname" runat="server" Text='<%# Bind("Firstname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Middlename" SortExpression="Middlename">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Middlename") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblmiddlename" runat="server" Text='<%# Bind("Middlename") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lastname" SortExpression="Lastname">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Lastname") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbllastname" runat="server" Text='<%# Bind("Lastname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age" SortExpression="Age">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblage" runat="server" Text='<%# Bind("Age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sex" SortExpression="Sex">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Sex") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblsex" runat="server" Text='<%# Bind("Sex") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" SortExpression="ID">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Friendswith" SortExpression="Friendswith">
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Friendswith") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblfriendswith" runat="server" Text='<%# Bind("Friendswith") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount" SortExpression="Amount">
<EditItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("Amount") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblamount" runat="server" Text='<%# Bind("Amount") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User_Firstname">
<ItemTemplate>
<asp:Label ID="lblfirstname1" runat="server" Text='<%# Bind("Firstname1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User_Middlename">
<ItemTemplate>
<asp:Label ID="lblmiddlename1" runat="server" Text='<%# Bind("Middlename1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User_Lastname">
<ItemTemplate>
<asp:Label ID="lbllastname1" runat="server" Text='<%# Bind("Lastname1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User_ID">
<ItemTemplate>
<asp:Label ID="lblid1" runat="server" Text='<%# Bind("ID1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="YOUR FRIENDS">
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Firstname" SortExpression="Firstname">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Firstname1") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblfirstname2" runat="server" Text='<%# Bind("Firstname1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Middlename" SortExpression="Middlename">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Middlename1") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblmiddlename2" runat="server" Text='<%# Bind("Middlename1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lastname" SortExpression="Lastname">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Lastname1") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbllastname2" runat="server" Text='<%# Bind("Lastname1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount" SortExpression="Amount">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Amount") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblamount2" runat="server" Text='<%# Bind("Amount") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Friendswith_gv2">
<ItemTemplate>
<asp:Label ID="lblfriendswith2" runat="server" Text='<%# Bind("Friendswith") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
If you don't need the duplicate data Use distinct in query.
So your SqlCommand should look like,
SqlCommand cmd = new SqlCommand("SELECT DISTINCT a.*,b.ID,b.Firstname,b.Middlename,b.Lastname,b.Friendswith,b.Amount from [User_TBL_DB] a LEFT OUTER JOIN [Friends_TBL_DB] b ON a.ID=b.Friendswith", conn);
The problem is not your GridView control, the data binded to the GridView is the real issue, since the task of the GridView is just to show data. Performing a LEFT OUTER JOIN you're getting a lot of duplicates. Use the DISTINCT statement to remove duplicates:
SqlCommand cmd = new SqlCommand("SELECT DISTINCT a.*,b.ID,b.Firstname,b.Middlename,b.Lastname,b.Friendswith,b.Amount from [User_TBL_DB] a LEFT OUTER JOIN [Friends_TBL_DB] b ON a.ID=b.Friendswith", conn);
I am trying to design gridview inside gridview with insert update and delete but in this i cant insert record. Can anyone Help?
Here is database table
1) tbl_SShow(ShowTimeId{P-Auto inc.},TheatreId,MovieId,FromDate,ToDate)
2) tbl_Show(ShowID{P-Auto inc.},ShowTime,Silver,Gold, Platinum,ShowTimeId)
3) tbl_Theatre(TheatreId{P-Auto inc.}, TName, Taddress)
and here is aspx page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script language=javascript type="text/javascript">
function expandcollapse(obj,row)
{
var div = document.getElementById(obj);
var img = document.getElementById('img' + obj);
if (div.style.display == "none")
{
div.style.display = "block";
if (row == 'alt')
{
img.src = "minus.gif";
}
else
{
img.src = "minus.gif";
}
img.alt = "Close to view other Customers";
}
else
{
div.style.display = "none";
if (row == 'alt')
{
img.src = "plus.gif";
}
else
{
img.src = "plus.gif";
}
img.alt = "Expand to show Orders";
}
}
</script>
</head>
<body>
<form id="AddEventForm" runat="server">
<ul id="tabsmenu1" class="tabsmenu">
<li class="active">Add Show Time</li>
</ul>
<div class="form">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" BackColor="#f1f1f1"
AutoGenerateColumns=false DataSourceID="SqlDataSource1" DataKeyNames="MovieID"
GridLines=None OnRowDataBound="GridView1_RowDataBound"
OnRowCommand = "GridView1_RowCommand" OnRowUpdating = "GridView1_RowUpdating" BorderStyle=Outset
OnRowDeleting = "GridView1_RowDeleting" OnRowDeleted = "GridView1_RowDeleted"
OnRowUpdated = "GridView1_RowUpdated" AllowSorting=true
Width="80%" >
<RowStyle BackColor="Gainsboro" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#0083C1" ForeColor="White"/>
<FooterStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="javascript:expandcollapse('div<%# Eval("ShowTimeId") %>', 'one');">
<img id="imgdiv<%# Eval("ShowTimeId") %>" alt="Click to show/hide Orders for Customer <%# Eval("ShowTimeId") %>" width="9px" border="0" src="../images/plus.gif"/>
</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Theatre Id" SortExpression="TheatreId">
<ItemTemplate>
<asp:Label ID="lblShowTimeId" Text='<%# Eval("ShowTimeId") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblShowTimeId" Text='<%# Eval("ShowTimeId") %>' runat="server"></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Theatre Id" SortExpression="TheatreId">
<ItemTemplate>
<asp:Label ID="lblTheatreId" Text='<%# Eval("TheatreId") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblTheatreId" Text='<%# Eval("TheatreId") %>' runat="server"></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlTheatre" runat="server" CssClass="form_input" AutoPostBack="True" TabIndex="1">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Movie Id" SortExpression="MovieId">
<ItemTemplate><%# Eval("MovieId")%></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlMovie" runat="server" CssClass="form_input" AutoPostBack="True" TabIndex="2">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlMovie" runat="server" CssClass="form_input" AutoPostBack="True" TabIndex="2">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="From Date" SortExpression="FromDate">
<ItemTemplate><%# Eval("FromDate")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFromDate" Text='<%# Eval("FromDate") %>' runat="server"></asp:TextBox>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/cal/calendar.gif" AlternateText="Click to show calendar" TabIndex="3" />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtFromDate" Format="dd/MM/yyyy" PopupButtonID="ImageButton1">
</asp:CalendarExtender>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFromDate" Text='' runat="server"></asp:TextBox>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/cal/calendar.gif" AlternateText="Click to show calendar" TabIndex="3" />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtFromDate" Format="dd/MM/yyyy" PopupButtonID="ImageButton1">
</asp:CalendarExtender>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="To Date" SortExpression="ToDate">
<ItemTemplate><%# Eval("ToDate")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtToDate" Text='<%# Eval("ToDate") %>' runat="server"></asp:TextBox>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/cal/calendar.gif" AlternateText="Click to show calendar" TabIndex="3" />
<asp:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="txtToDate" Format="dd/MM/yyyy" PopupButtonID="ImageButton2">
</asp:CalendarExtender>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtToDate" Text='' runat="server"></asp:TextBox>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/cal/calendar.gif" AlternateText="Click to show calendar" TabIndex="3" />
<asp:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="txtToDate" Format="dd/MM/yyyy" PopupButtonID="ImageButton2">
</asp:CalendarExtender>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edit" ShowEditButton="True" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="linkDeleteShow" CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="linkAddShow" CommandName="AddShow" runat="server">Add</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="div<%# Eval("ShowTimeId") %>" style="display:none;position:relative;left:15px;OVERFLOW: auto;WIDTH:97%" >
<asp:GridView ID="GridView2" AllowPaging="True" AllowSorting="true" BackColor="White" Width=100% Font-Size=X-Small
AutoGenerateColumns="false" Font-Names="Verdana" DataKeyNames="ShowTimeId" runat="server" ShowFooter="true"
OnPageIndexChanging="GridView2_PageIndexChanging" OnRowUpdating = "GridView2_RowUpdating" CssClass="Gridview"
OnRowCommand = "GridView2_RowCommand" OnRowEditing = "GridView2_RowEditing" GridLines=None
OnRowUpdated = "GridView2_RowUpdated" OnRowCancelingEdit = "GridView2_CancelingEdit" OnRowDataBound = "GridView2_RowDataBound"
OnRowDeleting = "GridView2_RowDeleting" OnRowDeleted = "GridView2_RowDeleted" OnSorting = "GridView2_Sorting"
BorderStyle=Double BorderColor="#0083C1">
<RowStyle BackColor="Gainsboro" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#0083C1" ForeColor="White"/>
<FooterStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Show ID" SortExpression="ShowID">
<ItemTemplate>
<asp:Label ID="lblShowID" Text='<%# Eval("ShowID") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblShowID" Text='<%# Eval("ShowID") %>' runat="server"></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ShowTime" SortExpression="ShowTime">
<ItemTemplate><%# Eval("ShowTime")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtShowTime" Text='<%# Eval("ShowTime")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtShowTime" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Silver" SortExpression="Silver">
<ItemTemplate><%# Eval("Silver")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtSilver" Text='<%# Eval("Silver")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtSilver" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gold" SortExpression="Gold">
<ItemTemplate><%# Eval("Gold")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtGold" Text='<%# Eval("Gold")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtGold" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Platinum" SortExpression="Platinum">
<ItemTemplate><%# Eval("Platinum")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPlatinum" Text='<%# Eval("Platinum")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPlatinum" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edit" ShowEditButton="True" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="linkDeleteCust" CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="linkAddOrder" CommandName="AddShow" runat="server">Add</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Event_MovieConnectionString %>"
SelectCommand="SELECT s.[ShowTimeId], s.[TheatreId], s.[MovieId], s.[FromDate], s.[ToDate], s.[Created],
s.[Modified] FROM [tbl_SShow] s ORDER BY s.[ShowTimeId]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
Please give me a solutions
Thanks.
I have wired a gridSearchResults_RowUpdating event for everytime an update link button is clicked. What I want to do is loop through the row at the event rowindex and add the textbox value in the row cell to a string collection. I am having troubh accessing the textbox in the cell. If this doesn't make sense my code should help make things clearer.
HTML
<asp:TemplateField HeaderText="Title" SortExpression="Title">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# Eval("jobTitle") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTitle" runat="server" Text='<%# Bind("jobTitle") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress">
<ItemTemplate>
<asp:Label ID="lblEmailAddress" runat="server" Text='<%# Eval("EmailAddress") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmailAddress" runat="server" Text='<%# Bind("EmailAddress") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Telephone Number" SortExpression="TelephoneNumber">
<ItemTemplate>
<asp:Label ID="lblTelephoneNumber" runat="server" Text='<%# Eval("TelephoneNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTelephoneNumber" runat="server" Text='<%# Bind("TelephoneNumber") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company" SortExpression="Company">
<ItemTemplate>
<asp:Label ID="lblCompany" runat="server" Text='<%# Eval("Company") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCompany" runat="server" Text='<%# Bind("Company") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbEditLink" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbCancelLink" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
<asp:LinkButton ID="lbUpdateLink" runat="server" CausesValidation="False" CommandName="Update" Text="Update"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#
protected void gridSearchResults_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gridSearchResults.Rows[e.RowIndex];
foreach (gridviewrow c in gridSearchResults.Rows[e.RowIndex])
{
}
}
So does anyone know a good way of looping through the cells of a gridview row and grabbing the textbox value in the gridview row cell. Thanks
Try this:
foreach (TableCell cell in gv.Rows(e.RowIndex).Cells)
{
TextBox txt1 = (TextBox)cell.FindControl("txtControlName");
// or if you don't want to hard code the control name and assuming that there is only 1 control in the cell then use this:
TextBox txt2 = (TextBox)cell.Controls(0);
}
I have a regular gridview with insert,edit,update, delete operations as in below image:
Before inserting data to the DB, I am doing the validation on the textboxes if they are filled or empty like this:
if (txtNewID.Text == "")
{
string message = "Please enter ID";
ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "Alert", "alert('" + message + "')", true);
isValid = false;
}
But after clicking the OK button of the alert message, the values that i added in the required textboxes are getting lost. How to keep the values of the textboxes in gridview?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Code, Type" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCommand="GridView1_RowCommand" ShowFooter="True" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<tr>
<td class="style1" >
<asp:GridView ID="gridID" DataKeyNames="Tracker_ID , Current_Status" runat="server"
GridLines="Both" Width="600px" BorderColor="Black"
ShowFooter="true" AutoGenerateColumns="false"
onrowcancelingedit="gridID_RowCancelingEdit"
onrowediting="gridID_RowEditing" onrowcommand="gridID_RowCommand"
onrowdeleting="gridID_RowDeleting" onrowdatabound="gridID_RowDataBound" onrowupdating="gridID_RowUpdating"
>
<Columns>
<asp:TemplateField HeaderStyle-BackColor="#1B7AE0" HeaderStyle-Font-Size="10" HeaderText="ID#">
<EditItemTemplate>
<asp:TextBox ID="ID" BorderWidth="1" Text='<%# Eval("ID") %>' Width="50px" runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewID" BorderWidth="1" Width="50px" runat="server"></asp:TextBox> </FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblID" Font-Size="Smaller" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle BackColor="#1B7AE0" Font-Size="10pt"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID Description" HeaderStyle-BackColor="#1B7AE0" HeaderStyle-Font-Size="10">
<EditItemTemplate>
<asp:TextBox ID="IDDesc" Text='<%# Eval("ID_Description") %>' Width="99px" BorderWidth="1" runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewIDDesc" BorderWidth="1" Width="99px" runat="server"></asp:TextBox> </FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblIDDesc" Font-Size="Smaller" Text='<%# Bind("ID_Description") %>' runat="server"></asp:Label>
</ItemTemplate>
<HeaderStyle BackColor="#1B7AE0" Font-Size="10pt"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="HCL Author" HeaderStyle-BackColor="#1B7AE0" HeaderStyle-Font-Size="10">
<EditItemTemplate>
<asp:TextBox ID="txtHclAuthor" Text='<%# Eval("HCL_Author") %>' Width="95px" BorderWidth="1" runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewHclAuthor" BorderWidth="1" Width="95px" runat="server"></asp:TextBox> </FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblHclAuthor" Font-Size="Smaller" Text='<%# Bind("HCL_Author") %>' runat="server"></asp:Label>
</ItemTemplate>
<HeaderStyle BackColor="#1B7AE0" Font-Size="10pt"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Xerox Author" HeaderStyle-BackColor="#1B7AE0" HeaderStyle-Font-Size="10">
<EditItemTemplate>
<asp:TextBox ID="txtXeroxAuthor" Text='<%# Eval("Xerox_Author") %>' Width="95px" BorderWidth="1" runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewXeroxAuthor" BorderWidth="1" Width="95px" runat="server"></asp:TextBox> </FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblXeroxAuthor" Font-Size="Smaller" Text='<%# Bind("Xerox_Author") %>' runat="server"></asp:Label>
</ItemTemplate>
<HeaderStyle BackColor="#1B7AE0" Font-Size="10pt"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Track" HeaderStyle-BackColor="#1B7AE0" HeaderStyle-Font-Size="10">
<EditItemTemplate>
<asp:TextBox ID="IDTrack" Text='<%# Eval("Track") %>' Width="80px" BorderWidth="1" runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewIDTrack" BorderWidth="1" Width="80px" runat="server"></asp:TextBox> </FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblIDTrack" Font-Size="Smaller" Text='<%# Bind("Track") %>' runat="server"></asp:Label>
</ItemTemplate>
<HeaderStyle BackColor="#1B7AE0" Font-Size="10pt"></HeaderStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td> </tr> </ContentTemplate>
</asp:UpdatePanel>
Whenever PostBack event is called then we can loss the page data.Did you place the grid in updatepanel otherwise place it in grid if you didn't get maintain the values in viewstate.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.requiredfieldvalidator.aspx
Bind the validator to the textbox you need to be required before a submit/post is valid using the ControlToValidate property.
Try to find required field validator dynamically on RowDataBoundEvent of Grid View.
TextBox tb=e.Row.FindControl("txtBox") as TextBox;
tb.ValidationGroup=e.RowIndex;
RequiredFieldvalidation rv=e.Row.FindControl("rv") as
RequiredFieldValidator; rv.ControlTovalidate=tb; rv.ErrorMessage;
rv.ErrorMessage="*";
rv.ValidationGroup=e.RowIndex;