I have 3 textboxes , 1 dropdown , 1 Button and 1 gridview .
Textboxes ID are :
ACode , ADesc , ASeq
DropDown ID is :
CPhase
Button ID is :
AddRowBTN
GridView ID is :
PhasesTempGrid
Now whats the story is ?
After adding some data into textboxes , when a user clicks ADDRow BTN , that data gets saved into datatable , then that datatable gets binded to gridview and it shows my textboxes data in it .
What i want to achieve ?
In my gridview i am use templateField and inside that template field there are textboxes ( Let the users edit the row without clicking any button ) .
Take an example of dummy data and problem persisting in there :
for e.g i put some data in textboxes :
ACode = "ABCD"
ADesc = "EFGH"
ASeq = "HIJK"
Then i click Add BTN , it saves the data in datatable and show in gridview , now if i do this in gridview :
ACode = "Edited ABCD"
ADesc = "Edited EFGH too"
ASeq = "WoW There"
Then i click Add BTN , it removes the edited record in gridview and shows original data which had came earlier from textboxes :
ACode = "ABCD"
ADesc = "EFGH"
ASeq = "HIJK"
I don't want that to be done ... Simple is that user can enter data from textboxes , click the btn to submit into grid then he can edit the data like he want ... data must not get flashed , here's my coding i have done so far :
public void AddNewData()
{
var dt = new DataTable();
if (ViewState["myDatatable"] != null)
{
dt = (DataTable) ViewState["myDatatable"];
}
else
{
dt.Columns.Add("CPhase");
dt.Columns.Add("ACode");
dt.Columns.Add("ADesc");
dt.Columns.Add("ASeq");
}
DataRow drow = dt.NewRow();
drow["CPhase"] = PhasesDrpDown.SelectedItem.Text;
drow["ACode"] = ACode.Text;
drow["ADesc"] = ADesc.Text.Trim();
drow["ASeq"] = ActSeq.Value.Trim();
dt.Rows.Add(drow);
ViewState["myDatatable"] = dt;
ACode.Text = string.Empty;
ADesc.Text = string.Empty;
ActSeq.Value = string.Empty;
}
public void BindMyGridview()
{
if (ViewState["myDatatable"] != null)
{
var dt = (DataTable) ViewState["myDatatable"];
if ((dt != null) && (dt.Rows.Count > 0))
{
var dv = new DataView();
dv = dt.DefaultView;
dv.Sort = "CPhase,ASeq,ACode";
PhasesTempGrid.DataSource = dt;
PhasesTempGrid.DataBind();
SearchPanel.Visible = true;
int tb = dt.Rows.Count;
TotalRows.Text = tb + " Activites";
}
}
}
protected void AddRowBTN_OnServerClick(object sender, EventArgs e)
{
AddNewData();
BindMyGridview();
}
and in code-behind code is :
<asp:gridview ID="PhasesTempGrid" ClientIDMode="Static" allowpaging="True"
OnPageIndexChanging="PhasesTempGrid_OnPageIndexChanging" PageSize = "5" PagerStyle-CssClass="custompaging" AllowSorting="True"
runat="server" AutoGenerateColumns="False" CssClass="table table-bordered" BackColor="White" ForeColor="Black" BorderStyle="Groove"
ShowFooter="True">
<Columns>
<asp:TemplateField HeaderText="Activity Code">
<ItemTemplate>
<asp:Label ID="ACodeLabel" runat="server" Text='<%#Eval("ACode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Activity Sequence">
<ItemTemplate>
<asp:Label ID="ASeqLabel" runat="server" Text='<%#Eval("ASeq") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Construction Phase">
<ItemTemplate>
<asp:Label ID="CPhaseTempLabel" runat="server" Text='<%#Eval("CPhase") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Activity Description / Name">
<ItemTemplate>
<asp:TextBox ID="ADescLabel" runat="server" Text='<%#Eval("ADesc") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="RemoveRow" CssClass="btn btn-danger btn-block" OnClick="RemoveRow_OnClick" runat="server" >Remove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle CssClass="panel-heading" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<HeaderStyle CssClass="customGridViewHeader" ForeColor="black"/>
</asp:gridview>
here i havent added dropdown combo in gridview but you can do it so that it will save my time (Bind it just the way PhaseDropDown have bind)...