Click here to Skip to main content
12,064,855 members (42,986 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
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.:

<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 11-Jun-11 22:38pm
Edited 11-Jun-11 23:25pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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>
  Permalink  
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!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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();
        }
    }
}
  Permalink  
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..
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  
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...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Use ItemDataBound event of DataGrid, and there check the DataSource and bind the TextBox dynamically.
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

<%#(String.IsNullOrEmpty(Eval("col1").ToString()) ? Eval("col2") : Eval("col1"))%>
  Permalink  
v3

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160204.4 | Last Updated 13 Jan 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100