none
GridView DataBinding

    Question





  • string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Data\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; string SQL = "SELECT * FROM tblWorkers"; // Create ADO.NET objects. SqlConnection con = new SqlConnection(connectionString); SqlCommand com = new SqlCommand(SQL, con); SqlDataAdapter adapter = new SqlDataAdapter(com); DataSet ds = new DataSet("Workers"); // Execute the command. try { con.Open(); Console.WriteLine("yes connected"); adapter.Fill(ds, "Workers"); } catch (Exception err) { MessageBox.Show(err.ToString()); } finally { con.Close(); } DataView view = ds.Tables["Workers"].DefaultView; dataGrid1.DataSource = view;
    I have the following code to show the data in my gridview which I get it from sqlserver database, I would like to change my code so that I can seperate my GUI from database code I have done it ,but I don't get any result from the gridview since my DataSource is null in my GUI class, Can anyone help me with that?


    I have a method in my SQLHandler class which is like this:

    DataRow dRow = datasets1.Tables["Workers"].Rows[inc];
    nameRecords = datasets1.Tables["Workers"].DefaultView.ToString();
    
    and nameRecord is the property of the class which is passed like this in my GUI class:
    
    //I supposed this line should show the info
    
     dataGrid1.DataSource =sqlHandlerClass.NameRecords;


    Wednesday, January 27, 2010 1:56 AM

Answers

  • DataRow dRow = datasets1.Tables["Workers"].Rows[inc];
    
    nameRecords = datasets1.Tables["Workers"].DefaultView.ToString();
    
    
    
     dataGrid1.DataSource =sqlHandlerClass.NameRecords;
    
    



    The problem could be because you are setting ToString() for DefaultView. You code should be something like this for class

        public class SqlHandler
        {
            private DataView nameRecords;
    
            public DataView NameRecords
            {
                get { return nameRecords; }
                set { nameRecords = value; }
            }
    
            public void SaveRecord()
            {
                DataSet datasets1= null;
                nameRecords = datasets1.Tables["Workers"].DefaultView;
            }
    	
        }

    And for GUI Form you can write as below

          private void Form1_Load(object sender, EventArgs e)
            {
                SqlHandler handlerclass = new SqlHandler();
                datagridview1.DataSource = handlerclass.NameRecords;
            }



    Gaurav Khanna Visual Basic MVP
    • Marked as answer by Mary_Developer Wednesday, January 27, 2010 2:10 PM
    Wednesday, January 27, 2010 12:06 PM

All replies

  • DataRow dRow = datasets1.Tables["Workers"].Rows[inc];
    
    nameRecords = datasets1.Tables["Workers"].DefaultView.ToString();
    
    
    
     dataGrid1.DataSource =sqlHandlerClass.NameRecords;
    
    



    The problem could be because you are setting ToString() for DefaultView. You code should be something like this for class

        public class SqlHandler
        {
            private DataView nameRecords;
    
            public DataView NameRecords
            {
                get { return nameRecords; }
                set { nameRecords = value; }
            }
    
            public void SaveRecord()
            {
                DataSet datasets1= null;
                nameRecords = datasets1.Tables["Workers"].DefaultView;
            }
    	
        }

    And for GUI Form you can write as below

          private void Form1_Load(object sender, EventArgs e)
            {
                SqlHandler handlerclass = new SqlHandler();
                datagridview1.DataSource = handlerclass.NameRecords;
            }



    Gaurav Khanna Visual Basic MVP
    • Marked as answer by Mary_Developer Wednesday, January 27, 2010 2:10 PM
    Wednesday, January 27, 2010 12:06 PM
  • Thank you very much for your help,exactly I did a mistake and define it as a string.
    Wednesday, January 27, 2010 2:11 PM