Hi akhter,
Check this example. Now please take its reference and correct your code.
HTML
<table>
<tr>
<td>
<asp:TextBox ID="txtremarks" runat="server" Width="454px"></asp:TextBox>
</td>
</tr>
<tr>
<td>Payment Mode
<asp:DropDownList ID="ddlpaymode" CssClass="form-control js-example-placeholder-single" Width="150px" runat="server">
<asp:ListItem Value="Bank">Bank</asp:ListItem>
<asp:ListItem Value="Cash">Cash</asp:ListItem>
<asp:ListItem Value="Cash By Hand">Cash By Hand</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Bank
<asp:DropDownList ID="ddlbank" CssClass="form-control js-example-placeholder-single" Width="200px" runat="server">
<asp:ListItem Value="1">SBI</asp:ListItem>
<asp:ListItem Value="2">AXIS</asp:ListItem>
<asp:ListItem Value="3">HDFC</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td >Location
<asp:DropDownList ID="ddlloc" CssClass="form-control js-example-placeholder-single" Width="200px" runat="server">
<asp:ListItem Value="1">Andheri</asp:ListItem>
<asp:ListItem Value="2">Malad</asp:ListItem>
<asp:ListItem Value="3">Goregaon</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Cheque No:
<asp:TextBox ID="txtchecqno" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Date:
<asp:TextBox ID="txtdate" TextMode="Date" runat="server"></asp:TextBox>
<br />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btn_add" runat="server" Text="Add" OnClick="btn_add_Click" Width="62px" />
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gvv" runat="server" AutoGenerateColumns="False" DataKeyNames="Index" BackColor="White"
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" CssClass="myGridClass"
Font-Names="Arial" GridLines="Horizontal" Height="198px" Style="margin-left: 3px" Width="807px">
<AlternatingRowStyle BackColor="#F7F7F7" />
<Columns>
<asp:TemplateField HeaderText="Sr No" HeaderStyle-Width="5%" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Payment_Mode">
<ItemTemplate>
<asp:Label ID="Payment_Mode" runat="server" Text='<%#Bind("Payment_Mode")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="BankID" Visible="false">
<ItemTemplate>
<asp:Label ID="Bank_ID" runat="server" Text='<%#Bind("Bank_ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bank" Visible="true">
<ItemTemplate>
<asp:Label ID="Bank_Name" runat="server" Text='<%#Bind("Bank_Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LocID" Visible="false">
<ItemTemplate>
<asp:Label ID="Loc_ID" runat="server" Text='<%#Bind("Loc_ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" Visible="true">
<ItemTemplate>
<asp:Label ID="Loc_Name" runat="server" Text='<%#Bind("Loc_Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cheque_NO">
<ItemTemplate>
<asp:Label ID="Cheque_NO" runat="server" Text='<%#Bind("Cheque_NO")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label ID="Chq_Date" runat="server" Text='<%#Bind("Chq_Date")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
</td>
</tr>
</table>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (ViewState["dt"] == null)
{
DataTable dt = new DataTable();
dt.Columns.Add("Index");
dt.Columns.Add("Payment_Mode");
dt.Columns.Add("Bank_ID");
dt.Columns.Add("Bank_Name");
dt.Columns.Add("Loc_ID");
dt.Columns.Add("Loc_Name");
dt.Columns.Add("Chq_Date");
dt.Columns.Add("Cheque_NO");
ViewState["dt"] = dt;
}
}
}
protected void btn_add_Click(object sender, EventArgs e)
{
if (ddlpaymode.SelectedItem != null && ddlbank.SelectedItem != null)
{
// int codeitem = Convert.ToInt32(DDLitem.SelectedItem.Value.ToString());
string Descriptionitem = ddlpaymode.SelectedItem.Text.ToString();
int bank_ID = Convert.ToInt32(ddlbank.SelectedItem.Value.ToString());
string Bank_Name = ddlbank.SelectedItem.Text.ToString();
int loc_ID = Convert.ToInt32(ddlloc.SelectedItem.Value.ToString());
string Loc_Name = ddlloc.SelectedItem.Text.ToString();
DataTable dtgridview = ViewState["dt"] as DataTable;
DataRow dr2 = dtgridview.NewRow();
bool ifExist = false;
if (!ifExist)
{
dr2["Index"] = dtgridview.Rows.Count + 1;
dr2["Payment_Mode"] = ddlpaymode.Text;
dr2["Bank_ID"] = bank_ID;
dr2["Bank_Name"] = Bank_Name;
dr2["Loc_ID"] = loc_ID;
dr2["Loc_Name"] = Loc_Name;
dr2["Chq_Date"] = txtdate.Text;
dr2["Cheque_NO"] = txtchecqno.Text;
// dr2["Time"] = DateTime.Now.ToString("HH:mm:ss");
dtgridview.Rows.Add(dr2);
DataView dv = dtgridview.DefaultView;
// dv.Sort = "Time Desc";
ViewState["dt"] = dtgridview;
gvv.DataSource = dtgridview;
gvv.DataBind();
string remark = "";
for (int i = 0; i < dtgridview.Rows.Count; i++)
{
string mode = dtgridview.Rows[i]["Payment_Mode"].ToString();
string bank = dtgridview.Rows[i]["Bank_Name"].ToString();
string cheque = dtgridview.Rows[i]["Cheque_NO"].ToString();
remark += mode + "," + bank + ",";
if (!string.IsNullOrEmpty(cheque))
{
remark += cheque + ",";
}
remark += " ";
}
txtremarks.Text = remark;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
If ViewState("dt") Is Nothing Then
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Index")
dt.Columns.Add("Payment_Mode")
dt.Columns.Add("Bank_ID")
dt.Columns.Add("Bank_Name")
dt.Columns.Add("Loc_ID")
dt.Columns.Add("Loc_Name")
dt.Columns.Add("Chq_Date")
dt.Columns.Add("Cheque_NO")
ViewState("dt") = dt
End If
End If
End Sub
Protected Sub btn_add_Click(ByVal sender As Object, ByVal e As EventArgs)
If ddlpaymode.SelectedItem IsNot Nothing AndAlso ddlbank.SelectedItem IsNot Nothing Then
Dim Descriptionitem As String = ddlpaymode.SelectedItem.Text.ToString()
Dim bank_ID As Integer = Convert.ToInt32(ddlbank.SelectedItem.Value.ToString())
Dim Bank_Name As String = ddlbank.SelectedItem.Text.ToString()
Dim loc_ID As Integer = Convert.ToInt32(ddlloc.SelectedItem.Value.ToString())
Dim Loc_Name As String = ddlloc.SelectedItem.Text.ToString()
Dim dtgridview As DataTable = TryCast(ViewState("dt"), DataTable)
Dim dr2 As DataRow = dtgridview.NewRow()
Dim ifExist As Boolean = False
If Not ifExist Then
dr2("Index") = dtgridview.Rows.Count + 1
dr2("Payment_Mode") = ddlpaymode.Text
dr2("Bank_ID") = bank_ID
dr2("Bank_Name") = Bank_Name
dr2("Loc_ID") = loc_ID
dr2("Loc_Name") = Loc_Name
dr2("Chq_Date") = txtdate.Text
dr2("Cheque_NO") = txtchecqno.Text
dtgridview.Rows.Add(dr2)
Dim dv As DataView = dtgridview.DefaultView
ViewState("dt") = dtgridview
gvv.DataSource = dtgridview
gvv.DataBind()
Dim remark As String = ""
For i As Integer = 0 To dtgridview.Rows.Count - 1
Dim mode As String = dtgridview.Rows(i)("Payment_Mode").ToString()
Dim bank As String = dtgridview.Rows(i)("Bank_Name").ToString()
Dim cheque As String = dtgridview.Rows(i)("Cheque_NO").ToString()
remark += mode & "," & bank & ","
If Not String.IsNullOrEmpty(cheque) Then
remark += cheque & ","
End If
remark += " "
Next
txtremarks.Text = remark
End If
End If
End Sub