locked
How to show gridview column data in a textbox

    Question

  • Hello all,

    I had posted my problem some months ago but I did not get any appropriate answer for it.
    My problem is: I want to retrieve data from a gridview column in a textbox, this column should not be visible in the gridview and the data in the textbox is just read only.

    I need to solve this problem programmatically all in code, C# code.
    so far the gridview is having AutogeneratedButton and few columns and the scenario is such that when the AutogeneratedButton is clicked it should show the column content in the textBox. for example if there will be 2 columns and a AutogeneratedButton
    Show Name(visible in gridview) Details(not to be shown in gridview)
    when show for specific Name is clicked to show the details in the textbox (as READ ONLY)



    I have been stuck on this problem for months, any help will be appreciated
    Thanks again



    Tuesday, August 18, 2009 9:07 AM

Answers

All replies

  • If you are talking about GridView of ASP.NET then try this.. In the event handler of Show button.. Try with this..

    textBox1.Text = gridView1.SelectedRow.Cells[2].Text;
    textBox1.ReadOnly = True;


    If you are talking about DataGridView of Windows Forms then let me know.

    Life would have been much easier if I had the source-code !!
    Tuesday, August 18, 2009 9:52 AM
  • I'm doing this with cSharp.

     

    and my code for this part is :

       void gridv_SelectedIndexChanged(object sender, EventArgs e)
            {
              
                GridViewRow row = gridView1.SelectedRow;
                txtBox.Text = row.Cells[2].Text;
                gridView1.DataBind();
      
            }

    it does retireve data from the column when I show the column in the gridview when I remove or hide the column then it doesn't get the data!!
    And in my task I should not show the column in the gridview just to get the data.

    Tuesday, August 18, 2009 9:57 AM
  • Hii,
      Your problem is not really well described. As far as I could understand, an easy workaround could be to use the datatable or any dataobject that you are using as the datasource of your gridview.When you click the show button. from the eventargument e, retrieve the row index and with that row index..you can retrieve the cell value from the datatable, as you know the column name.It doesn't matter wheter the column is shown in the grid or not.

    Tuesday, August 18, 2009 9:59 AM
  • Hi,
    i hope it will help you...
    private void Form1_Load(object sender, EventArgs e)
    {
    	AddColumns();		
    	dataGridView2.CellClick += new DataGridViewCellEventHandler(dataGridView2_CellClick);
    	textBox1.ReadOnly = true;
    }
    
    void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
    {
    	if (e.ColumnIndex == 0)
    	{
    		textBox1.Text = dataGridView2.Rows[e.RowIndex].Cells[2].Value.ToString();
    	}
    }
    
    private void AddColumns()
    {
    	DataGridViewButtonColumn column1 = new DataGridViewButtonColumn();
    	column1.HeaderText = "Treatement";
    	column1.Width = 90;           
    	DataGridViewTextBoxColumn column3 = new DataGridViewTextBoxColumn();
    	column3.HeaderText = "Action1";
    	column3.Width = 90;
    	DataGridViewTextBoxColumn column4 = new DataGridViewTextBoxColumn();
    	column4.HeaderText = "Action2";
    	column4.Width = 90;
    	dataGridView2.Columns.Add(column1);
    	dataGridView2.Columns.Add(column3);
    	dataGridView2.Columns.Add(column4);
    
    }

    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
    Tuesday, August 18, 2009 10:03 AM
  • Hi,
    Can you post the code snippet which you are using, or haven't you started at all.

    Regards,
    Vishal Mohan
    Tuesday, August 18, 2009 10:11 AM
  • If you are trying to get a hidden column, you probably need to use the name, not number.
    Tuesday, August 18, 2009 7:10 PM
  • would you please tel me how to specify the name??
    Wednesday, August 19, 2009 10:07 AM
  • Hi, it should look like this sample
    Wednesday, August 19, 2009 12:48 PM
  • I have sent the code, Any solution regarding my problem??
    Wednesday, August 19, 2009 12:49 PM
  • Hi SoftProgrammerr,

    Could you please try the solution in the following similar issue?
    http://forums.asp.net/p/1106692/1694814.aspx
    http://forums.asp.net/p/1458599/3345831.aspx
    http://www.codeproject.com/KB/cs/Hide_GridView_Column_Cell.aspx

    Another option for you:
    You also can retrieve the data from the database via SQL when Select button is clicked.

    BTW, this forum is for the support of Visual C# General. Since your issue is related to ASP.NET, you could post your thread on ASP.NET Forum . This will make answer searching in the forum easier and be beneficial to other community members as well.

    Hope this helps.

    Best regards,
    Yichun Chen

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, August 21, 2009 3:58 AM
  • hi YiChun Chen,

    thanks for given links! I'm checking them to see If I can solve my pro.
    regarding the forum! My problem is related to C# not asp.net! thats why I'm here!

    anyway , thanks again!
    Friday, August 21, 2009 7:56 AM
  • Hi SoftProgrammerr,

    It's my pleasure.

    Hope your issue will be solved ASAP. :)

    Best regards,
    Yichun Chen
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, August 21, 2009 7:59 AM
  • Hi again YiChun Chen,

    Thanks for the given links they were very helpful.
    I think I found the solution.

    Thanks to all
    Friday, August 21, 2009 1:40 PM
  • Hi SoftProgrammerr,

    I'm so glad to hear that. You're welcome. :)

    Have a good week!

    Best regards,
    Yichun Chen
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, August 24, 2009 1:22 AM