Click here to Skip to main content
16,019,764 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how i can fill the values in textbox with today's date in gridview?

textbox is present inside the gridview..

i am using asp.net,c#,VS2005 and sql server 2005

thnk u once again...

regards
karan
Posted
Updated 9-May-11 0:56am
v2

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            onrowdatabound="GridView1_RowDataBound" >
            <Columns>
                 <asp:TemplateField HeaderText="First">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1"  runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Second">
                    <ItemTemplate>
                        <asp:DropDownList ID="ddl_BindValues" DataValueField="Name" runat="server">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Third">
                    <ItemTemplate>
                       <asp:TextBox ID="TextBox2" Text='<%#Eval("id") %>' runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Fourth">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox3" Text='<%#Eval("name") %>' runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Fifth">
                    <ItemTemplate>
                       <asp:TextBox ID="TextBox4" Text='<%#Eval("Price") %>' runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True" HeaderText="Edit" />
            </Columns>
        </asp:GridView>





MSIL
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        public void BindGrid()
        {
            Con = new SqlConnection("Connection string");
            Con.Open();
             dt = new SqlDataAdapter("Select * from Your Table ", Con);
            DataSet ds = new DataSet();
            dt.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            Con.Close();
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox txt1 = (TextBox)e.Row.FindControl("TextBox1");
                txt1.Text = DateTime.Now.ToString();
            }
        }



<big>I hope now you can solve that</big></i> 
 
Share this answer
 
Comments
karan joshua 9-May-11 7:58am    
oh great...

I wasted 4hrs to do this..


Thank U.. Thank U.. Thank U.. Thank U.. Thank U.. Thank U.. Thank U.. Thank U.. Thank U..

thank u very much

my vote is 5/5...thank u once again
Mahendra.p25 9-May-11 8:05am    
welcome
Member 14709933 24-Jan-20 6:17am    
i have one textbox by which i will get the no of days from user after getting the no of dates that much the number of text boxes should be visible to enter the code using gridview
currTextbox.Text = DateTime.Now.ToString();

Find the textbox at row level and just write the above code line!

You need to use GridView RowDataBound[^] event method in order to get access at row level.

Try!?
 
Share this answer
 
v2
Comments
karan joshua 9-May-11 7:13am    
textbox is present inside the gridview..
Sandeep Mewara 9-May-11 7:31am    
So what? There is an event called RowDataBound. Try that.
Hi karan joshua,

I think you can use this code :

dataGridView1[ColumnIndex, RowIndex].Value = DateTime.Now.ToString();


where ColumnIndex and RowIndex are integer values defining the index of the cell to be editted.

I hope this help,
:)
 
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