Click here to Skip to main content
Rate this: bad
good
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 21:38pm
Edited 11-Jun-11 22: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 at 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 at 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 at 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 at 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 at 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  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 352
1 OriginalGriff 344
2 Abdul Samad KP 145
3 CPallini 140
4 Richard MacCutchan 110
0 OriginalGriff 6,329
1 Sergey Alexandrovich Kryukov 5,700
2 CPallini 4,940
3 George Jonsson 3,469
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 12 Jun 2011
Copyright © CodeProject, 1999-2014
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