Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using C#,asp.net 2.0,server 2005 and visual studio 2005

Can anybody help with the following problem:

initially i load gridview (contains 1 textbox) with some values using...


in aspx
<asp:TextBox Text='<%#Eval("col1") %>'/>

in C#:
 
Con.Open();
             SqlDataAdapter dt = new SqlDataAdapter("select col1 from Table1", Con);
             DataSet ds = new DataSet();
             dt.Fill(ds);
             GridView1.DataSource = ds;
             GridView1.DataBind();


now i click one buttton and when i click button the gridview should be loaded with some new values and i use the following function to bind the gridview.:

XML
<pre>


in C#:

Con.Open();
             SqlDataAdapter dt = new SqlDataAdapter("select col2 from Table1", Con);
             DataSet ds = new DataSet();
             dt.Fill(ds);
             GridView1.DataSource = ds;
             GridView1.DataBind();


</pre>


what i should write a common in
Text='<% %>'
, so that the gridview is binded with function specified values





please help me.
Thanks in Advance.
Regards,
Karan
Posted
Updated 24-Apr-19 1:58am
v2

Take a look there-CP Question-Answer[^] same question is replied with nice solutions.
You should write as
<asp:textbox id="YourControlID" runat="Server" xmlns:asp="#unknown">
          Text='<%# Bind("ColumnName") %>'  /></asp:textbox>
 
Share this answer
 
Comments
karan joshua 12-Jun-11 4:41am    
thank u for your link...
If i use say <pre>Text='<%# Bind("col1") %>'</pre>

next time time it will be <pre>Text='<%# Bind("col2") %>'</pre> in aspx page. but i cant change it to that when the application runs.

Any solution?
thatraja 12-Jun-11 6:16am    
Spot on, 5!
I have two tables
dt.Columns.Add("Col1");
dt.Rows.Add("Col11");
dt1.Columns.Add("Col2");
dt1.Rows.Add("Col12");

I am binding GridView1 first time with dt and second time with dt1 which are having different ColumnName.
And I am checking the DataSource property
public void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TextBox txt = (TextBox)e.Row.FindControl("txtCol");
        if (GridView1.DataSource == dt)
        {
            txt.Text = DataBinder.Eval(e.Row.DataItem, "Col1").ToString();
        }
        else if (GridView1.DataSource == dt1)
        {
            txt.Text = DataBinder.Eval(e.Row.DataItem, "Col2").ToString();
        }
    }
}
 
Share this answer
 
Comments
karan joshua 12-Jun-11 5:37am    
Thank you.. This one helped me a lot.... thank u very much....
i solved My problem using this...
My 5.

Thank u once again..
Hi,
You can use a common column alias name for sql queries and use that alias column name for data bind. Like try this code below,
in *.aspx
<asp:TextBox Text='<%#Eval("mycolumn") %>'/>

in *.aspx.cs
///For 1st DataBind
SqlDataAdapter dt = new SqlDataAdapter("select col1 as mycolumn from Table1", Con);
///For 2nd DataBind
SqlDataAdapter dt = new SqlDataAdapter("select col2 as mycolumn from Table1", Con);

as you can see both queries use same column alias, so switching data source for GridView wont effect the binding.

Hope this will help.
 
Share this answer
 
Comments
karan joshua 12-Jun-11 5:40am    
Oh nice solution.. thank u very much...Looks very simple...

Thank you very much...My 5..

Thank you once again...
Use ItemDataBound event of DataGrid, and there check the DataSource and bind the TextBox dynamically.
 
Share this answer
 
Comments
karan joshua 12-Jun-11 4:07am    
how to do that?
any code help?
My problem is if i use <pre>Text='<%#Eval("col1") %>'</pre> second time it will be col2.
use databind in the edittemplate of your gridview.

first, click the smart tag, then edit template.
then click on your textbox smart tag, click databind.
 
Share this answer
 
<%#(String.IsNullOrEmpty(Eval("col1").ToString()) ? Eval("col2") : Eval("col1"))%>
 
Share this answer
 
v3

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