12 Replies - 4175 Views - Last Post: 20 January 2012 - 01:48 PM Rate Topic: -----

#1 MichaelGrove  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-October 11

Can't populate textboxes with data from datagridview

Posted 06 January 2012 - 03:01 PM

Hi,
I'm having some problems with a phone book i am in the process of making (Trying to learn C# :-))

When i execute the code, and search for something in the phone book, i does return the results as supposed in the datagridview.

What i am having problems with, is that when i click on a row in the datagridview, i would like the data to be passed to the textboxes on the form. It does sometimes, but i have to click 2, 3, 4 or maybe 5 times on the row before something happens. Can anyone of you see something that i should change?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Phone_Tool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        System.Data.SqlClient.SqlConnection con;
        System.Data.SqlClient.SqlDataAdapter da;
        DataSet ds1;

        private void Form1_Load(object sender, EventArgs e)
        {
            con = new System.Data.SqlClient.SqlConnection();
            ds1 = new DataSet();

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = dataGridView1.SelectedCells[0].RowIndex;

            txtFirstname.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
            txtMiddelname.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
            txtLastname.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
            txtInitials.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
            txtEmployeeno.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
            txtDepartment.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();
            txtLocalno.Text = dataGridView1.Rows[i].Cells[7].Value.ToString();
            txtPhonenr.Text = dataGridView1.Rows[i].Cells[8].Value.ToString();
            txtEmail.Text = dataGridView1.Rows[i].Cells[9].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Me\\Documents\\DataB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";


            con.Open();

            
            string sql = "SELECT * FROM tblEmpl WHERE (tblEmpl.firstname LIKE \'" + txtSearch.Text + "%" + "\') OR (tblEmpl.middelname LIKE \'" + txtSearch.Text + "%" + "\') OR (tblEmpl.lastname LIKE \'" + txtSearch.Text + "%" + "\') OR (tblEmpl.initials LIKE \'" + txtSearch.Text + "%" + "\') OR (tblEmpl.employeeno LIKE \'" + txtSearch.Text + "%" + "\') OR (tblEmpl.department LIKE \'" + txtSearch.Text + "%" + "\') OR (tblEmpl.localno LIKE \'" + txtSearch.Text + "%" + "\') OR (tblEmpl.phoneno LIKE \'" + txtSearch.Text + "%" + "\') OR (tblEmpl.email LIKE \'" + txtSearch.Text + "%" + "\')";

            try
            {
                
                da = new System.Data.SqlClient.SqlDataAdapter(sql, con);

                
                da.Fill(ds1, "Employ");

                
                dataGridView1.DataSource = ds1.Tables[0];

                dataGridView1.Columns[1].HeaderText = "Fornavn";
                dataGridView1.Columns[3].HeaderText = "Efternavn";
                dataGridView1.Columns[4].HeaderText = "Initialer";

                
            }
            catch (System.Exception excep)
            {
                MessageBox.Show(excep.Message);
            }



            con.Close();


            con.Dispose();
        }
    }
}


Thanks in advance

Michael

Is This A Good Question/Topic? 0
  • +

Replies To: Can't populate textboxes with data from datagridview

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Can't populate textboxes with data from datagridview

Posted 06 January 2012 - 09:52 PM

Debugging 101:

put a breakpoint on line 32. Does this line get executed EVERY time you click on a row?
press F10 once so execution goes to line 34. What is the Value of 'i'? Does it match the index of the row you clicked on?

First read this
http://www.dreaminco...-common-errors/

Then see FAQ #5 for debugging tutorials


FAQ (Frequently Asked Questions - Updated Jan 2012

Spoiler

Was This Post Helpful? 1
  • +
  • -

#3 MichaelGrove  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-October 11

Re: Can't populate textboxes with data from datagridview

Posted 08 January 2012 - 01:37 AM

Hi tlhIn`toq,

I've tried to add the break point and run my program. When i click a row, it's not every time that it returns to the editor so that i can clicked f10. It's like the program don't see that i clicked on a row. Then i can click a few times, then it returns to the editor and i press f10, and the value of i is equal to the row i clicked.

I will read through the link og debugging tutorials you gave me.

Thanks
Michael
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Can't populate textboxes with data from datagridview

Posted 08 January 2012 - 07:26 AM

Sometimes you click on the control and it responds... and sometimes it doesn't respond.
That's a real problem.

My first guess is that everything in your application is happening on the GUI thread, and that something is going on that is keeping the application from being able to respond to user events like mouse clicks.


75 MessageBox.Show(excep.Message);
Could this messagebox be coming up but BEHIND your main application? That would explain why your app doesn't have focus.

Connect Timeout=30
30 seconds to connect to your local C: drive? If your application is having a hard time connecting then the GUI is going to be held up for 30 seconds at a time.
Was This Post Helpful? 0
  • +
  • -

#5 MichaelGrove  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-October 11

Re: Can't populate textboxes with data from datagridview

Posted 09 January 2012 - 11:13 AM

I don't see the message box anywhere when i use the application, so i don't think that's the problem. I also just tried to change the timeout to 15, 10, 5, 2 and 1 second, but it is still the same.

Michael
Was This Post Helpful? 0
  • +
  • -

#6 plejer_adam  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 90
  • Joined: 17-May 10

Re: Can't populate textboxes with data from datagridview

Posted 10 January 2012 - 05:03 AM

The problem is... you must click CELL CONTENT every time... not just row or cell..

Click on CONTENT and it will work every time ...

cheers
Was This Post Helpful? 1
  • +
  • -

#7 MichaelGrove  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-October 11

Re: Can't populate textboxes with data from datagridview

Posted 10 January 2012 - 08:08 AM

View Postplejer_adam, on 10 January 2012 - 05:03 AM, said:

The problem is... you must click CELL CONTENT every time... not just row or cell..

Click on CONTENT and it will work every time ...

cheers


Hi plejer_adam,

I don't understand what you mean, could you explain a bit more?

Michael
Was This Post Helpful? 0
  • +
  • -

#8 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,675
  • Joined: 24-August 11

Re: Can't populate textboxes with data from datagridview

Posted 10 January 2012 - 02:29 PM

plejer_adam is saying to make sure that you click on the actual text in the cell, not just the cell itself
Was This Post Helpful? 1
  • +
  • -

#9 MichaelGrove  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-October 11

Re: Can't populate textboxes with data from datagridview

Posted 12 January 2012 - 01:21 PM

Haha! That was the problem. It only populates the textboxes when i click directly on the content, not the cell.

Is there any way to make it work on cell click? so i don't have to click exactly on the content?

Thanks

Michael
Was This Post Helpful? 0
  • +
  • -

#10 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,675
  • Joined: 24-August 11

Re: Can't populate textboxes with data from datagridview

Posted 13 January 2012 - 10:06 AM

This is what I did with the gridviews on one of my work's sites. It allows you to click anywhere in the row. You have to use the autogenerated select button, but the C# and CSS hide it.

for your gridview:
<asp:GridView ID="myGridView" runat="server" 
    ClientIDMode="Static" 
    OnRowDataBound="myGridView_RowDataBound" 
    AutoGenerateSelectButton="true">
</asp:GridView>



then in code-behind:
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[0].Attributes.Add("class", "hideSelect");
    e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(myGridView, "Select$" + e.Row.RowIndex);
}



The second line is what's allowing you to click anywhere; the first row is what I use to hide the Autogenerated select button necessary for this to work, because I think it's ugly.

Then add:
.hideSelect {
	display: none;
}


to your CSS and you'll be good to go.

Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#11 MichaelGrove  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-October 11

Re: Can't populate textboxes with data from datagridview

Posted 18 January 2012 - 02:02 PM

Hi h4nnib4l

It's not a website, but a Win Form program i am making :)

I have spend much time trying different thing's and searching the internet, but no one seems to have a solution for this issue.

Michael
Was This Post Helpful? 0
  • +
  • -

#12 blogwhodle  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 18-January 12

Re: Can't populate textboxes with data from datagridview

Posted 19 January 2012 - 09:15 PM

View PostMichaelGrove, on 18 January 2012 - 02:02 PM, said:

Hi h4nnib4l

It's not a website, but a Win Form program i am making :)

I have spend much time trying different thing's and searching the internet, but no one seems to have a solution for this issue.

Michael

Not to be rude or anything, but I have tried making games with WinForms and found out that they are not the way to go for gaming. I would suggest using an actual game type of project instead of WinForms. (You can use what you feel like but just a suggestion) :hammer:
Was This Post Helpful? 0
  • +
  • -

#13 MichaelGrove  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-October 11

Re: Can't populate textboxes with data from datagridview

Posted 20 January 2012 - 01:48 PM

View Postblogwhodle, on 19 January 2012 - 09:15 PM, said:

Not to be rude or anything, but I have tried making games with WinForms and found out that they are not the way to go for gaming. I would suggest using an actual game type of project instead of WinForms. (You can use what you feel like but just a suggestion) :hammer:


A game? I am not trying to make a game, but a phone book :-) See post no. 1 :-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1