Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My Design as follows

Leave Type ddlleavetype
From Date txtdate (textbox)
To Date txttodate (textbox)
Applied date txtapplied (textbox)
Reason txtReason (textbox)

i am displaying above dropdownlist and textbox value to gridview

My code as follows

protected void btnview_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("LeaveType");
DataColumn dc2 = new DataColumn("FromDate");
DataColumn dc3 = new DataColumn("ToDate");
DataColumn dc4 = new DataColumn("AppliedOn");
DataColumn dc5 = new DataColumn("Reason");

dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
dt.Columns.Add(dc5);

DataRow dr1 = dt.NewRow();
dr1["LeaveType"] = ddlleavetype.SelectedValue.ToString();
dr1["FromDate"] = txtDate.Text;
dr1["ToDate"] = txttodate.Text;
dr1["AppliedOn"] = txtapplied.Text;
dr1["Reason"] = txtreason.Text;
dt.Rows.Add(dr1);
GridView1.DataSource = dt;
GridView1.DataBind();
}

when i run the above above code when i click the button,
in gridview textbox values not showing in gridview. It shows the empty in gridview.

What is the mistake in my above code

What I have tried:

.
Posted
Updated 24-Jan-18 17:22pm
v2
Comments
F-ES Sitecore 23-Jan-18 8:20am    
There's nothing wrong with the code, the problem lies with something you haven't posted. You really need to learn to debug your own code, you can't keep doing code-dumps on the internet and expect other people to fix your code. Step through it line by line and work out what is happening that shouldn't be, or what isn't happening that should.
[no name] 23-Jan-18 8:39am    
i debug my code in dt values comes , But in run mode gridview shows empty. i checked several times.

GridView1.DataSource = dt; //Values comes in dt shows all the 5 values. but in gridview shows empty . could please help me regarding this.
Karthik_Mahalingam 23-Jan-18 9:33am    
use  Reply   button to post comments/query to the concerned user, so that the user gets notified and respond to your text.
Laxmidhar tatwa technologies 23-Jan-18 9:24am    
chnage autogenerrated column true of grid
Vincent Maverick Durano 24-Jan-18 23:23pm    
AutogenerateColumns property is true by default AFAIK.

1 solution

Assuming that your TextBox and DropDown controls are outside GridView. Here's an example how to add rows in GridView based from server control values:

ASPX
ASP.NET
<asp:TextBox ID="TextBox1" runat="server"/>
<asp:TextBox ID="TextBox2" runat="server"/>
<asp:TextBox ID="TextBox3" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" >
</asp:GridView>


CODE BEHIND:

C#
//A method that will bind GridView based on the TextBox 
//values and retain its values on post backs.

private void BindGrid(int rowcount)
{
        DataTable dt = new DataTable();
        DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("TextBox1Column", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("TextBox2Column", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("TextBox3Column", typeof(String)));
 
        if (ViewState["CurrentData"] != null)
        {
            for (int i = 0; i < rowcount + 1; i++)
            {
                dt = (DataTable)ViewState["CurrentData"];
                if (dt.Rows.Count > 0)
                {
                    dr = dt.NewRow();
                    dr[0] = dt.Rows[0][0].ToString();
 
                }
            }
            dr = dt.NewRow();
            dr[0] = TextBox1.Text;
            dr[1] = TextBox2.Text;
            dr[2] = TextBox3.Text;
            dt.Rows.Add(dr);
 
        }
        else
        {
            dr = dt.NewRow();
            dr[0] = TextBox1.Text;
            dr[1] = TextBox2.Text;
            dr[2] = TextBox3.Text;
 
            dt.Rows.Add(dr);
 
        }
 
        // If ViewState has a data then use the value as the DataSource
        if (ViewState["CurrentData"] != null)
        {
            GridView1.DataSource = (DataTable)ViewState["CurrentData"];
            GridView1.DataBind();
        }
        else
        {
        // Bind GridView with the initial data assocaited in the DataTable
            GridView1.DataSource = dt;
            GridView1.DataBind();
 
        }
        // Store the DataTable in ViewState to retain the values
        ViewState["CurrentData"] = dt;
 
    }

 protected void Button1_Click(object sender, EventArgs e)
 {
        // Check if the ViewState has a data assoiciated within it. If
        if (ViewState["CurrentData"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentData"];
            int count = dt.Rows.Count;
            BindGrid(count);
        }
        else
        {
            BindGrid(1);
        }
        TextBox1.Text = string.Empty;
 
        TextBox1.Focus();
}


In case your server controls are within GridView and you want to generate rows dynamically, then here's a complete step-by-step example that I wrote a few years ago: https://www.codeproject.com/Articles/1109899/Dynamically-Adding-and-Deleting-Rows-in-GridView-a
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900