title: Python.NET - Sending data to a DataGridView
body: <p>To give some background, I am attempting to use Python to give some of my users an interface through a third-party program (that already has IronPython embedded) to add, edit, and delete records from a SQL Server database.</p>
  <p>I've received good assistance from one member <a href="https://stackoverflow.com/questions/66781011/python-net-winforms-how-to-pass-info-from-a-textbox-to-a-button-click-event">here</a> about Python .NET Winforms so I am attempting to get some more assistance. My goal is to populate a DataGridView (dgv) with records from a table. Seems pretty simple. However, when I run the code, my dgv is just an empty container.</p>
  <p>I could use some assistance or better yet, point me in the right direction to find the answer. I am willing to learn.</p>
  <pre><code>import clr
  
  clr.AddReference(&quot;System.Windows.Forms&quot;)
  clr.AddReference(&quot;System.Drawing&quot;)
  clr.AddReference(&quot;System.Data&quot;)
  clr.AddReference(&quot;System.Globalization&quot;)
  
  from System.Windows.Forms import *
  from System.Drawing import *
  from System.Data import *
  from System.Data.SqlClient import *
  from System.Globalization import *
  
  class MyForm(Form):
      def __init__(self):
  
          # Setup the form
          self.Text = &quot;Calculated Control Limits Form&quot;
          self.StartPosition = FormStartPosition.CenterScreen # https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.startposition?view=net-5.0
  
          # Create &amp;  configure the DataGridView label
          self.setupDataGridViewLabel()
  
          # Create, configure, and populate data for the DataGridView
          self.setupDataGridView()
          self.PopulateDataGridView(&quot;SELECT * FROM SPC_CALCULATED_CONTROL_LIMITS&quot;)
  
          # Create &amp; configure a line seperator
          self.setupSeperator()
          
          # Create &amp; configure a Label to grab the users Employee ID
          self.setupEmployeeIDLabel()
          
          # Create &amp; configure a label for the Lower Control Limit Label
          self.Load_LCL_Label()
          
          # Create &amp; configure a TextBox for the Lower Control Limit
          self.Load_LCL_TextBox()
          
          # Create &amp; configure a label for the Target Mean
          self.LoadTargetMeanLabel()
          
          # Create &amp; configure a TextBox for the Target Mean
          self.LoadTargetMeanTextBox()
          
          # Create &amp; configure a label for the Upper Control Limit
          self.Load_UCL_Label()
          
          # Create &amp; configure a TextBox for the Upper Control Limit
          self.Load_UCL_TextBox()
          
          # Create &amp; configure a label for the Comment
          self.LoadCommentLabel()
          
          # Create a TextBox for the Comment
          self.LoadCommentTextBox()
          
          # Create &amp; configure the button
          self.LoadButton()
  
  #       Register the event handler
          self.button.Click += self.button_Click
          
          self.Size = Size (550, self.button.Bottom + self.button.Height + 15)
          
          #Load the Controls to the Form
          self.Controls.Add(self.lblCCL)
          self.Controls.Add(self.dgvCCL)
          
          self.Controls.Add(self.button)
          
          self.Controls.Add(self.lblSep)
          
          self.Controls.Add(self.lblEmpID)
          
          self.Controls.Add(self.lblLCL)
          self.Controls.Add(self.txtLCL)
          
          self.Controls.Add(self.lblTarget)
          self.Controls.Add(self.txtTarget)
          
          self.Controls.Add(self.lblUCL)
          self.Controls.Add(self.txtUCL)
          
          self.Controls.Add(self.lblComment)
          self.Controls.Add(self.txtComment)
  
      def button_Click(self, sender, args):
          self.Close()
          
      def setupDataGridView(self):            
          self.dgvCCL = DataGridView()
          self.dgvCCL.AllowUserToOrderColumns = True
          self.dgvCCL.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
          self.dgvCCL.Location = Point(self.lblCCL.Left, self.lblCCL.Bottom + 5)
          self.dgvCCL.Size = Size(500, 250)
          self.dgvCCL.TabIndex = 3
          #self.dgvCCL.ColumnCount = len(headers)
          self.dgvCCL.ColumnHeadersVisible = True
          
      def setupSeperator(self):
          self.lblSep = Label()
          self.lblSep.Size = Size(500, 2)
          self.lblSep.BackColor = Color.DarkGray
          self.lblSep.BorderStyle = BorderStyle.Fixed3D
          self.lblSep.Location = Point(self.dgvCCL.Left, self.dgvCCL.Bottom + 10)
          
      def setupDataGridViewLabel(self):
          self.lblCCL = Label()
          self.lblCCL.Text = &quot;Calculated Control Limits View&quot;
          self.lblCCL.Font = Font(self.Font, FontStyle.Bold)
          self.lblCCL.Location = Point(10, 10)
          self.lblCCL.BorderStyle  = 0 # BorderStyle.None --&gt; https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.borderstyle?view=net-5.0
          self.lblCCL.Size = Size(self.lblCCL.PreferredWidth, self.lblCCL.PreferredHeight)
  
      def setupEmployeeIDLabel(self):
          self.lblEmpID = Label()
          self.lblEmpID.Text = 'login.computeruser.upper()'
          self.lblEmpID.Font = Font(self.Font, FontStyle.Bold)
          self.lblEmpID.Size = Size(self.lblEmpID.PreferredWidth, self.lblEmpID.PreferredHeight)
          self.lblEmpID.Location = Point(self.dgvCCL.Right - self.lblEmpID.Width, self.dgvCCL.Top - 15)
          self.lblEmpID.BorderStyle = 0
  
      def Load_LCL_Label(self):
          self.lblLCL = Label()
          self.lblLCL.Text = 'Lower Control Limit'
          self.lblLCL.Size = Size(self.lblLCL.PreferredWidth, self.lblLCL.PreferredHeight)
          self.lblLCL.Location = Point(self.lblSep.Left + 65, self.lblSep.Top + 15)
          self.lblLCL.BorderStyle = 0
  
      def Load_LCL_TextBox(self):
          self.txtLCL = TextBox()
          self.txtLCL.Size = Size(100, 50)
          self.txtLCL.Location = Point(self.lblLCL.Left, self.lblLCL.Bottom + 3)
  
      def LoadTargetMeanLabel(self):
          self.lblTarget = Label()
          self.lblTarget.Text = 'Target Mean'
          self.lblTarget.Size = Size(self.lblLCL.PreferredWidth, self.lblLCL.PreferredHeight)
          self.lblTarget.Location = Point(self.lblLCL.Left + 125, self.lblSep.Top + 15)
          self.lblTarget.BorderStyle = 0
  
      def LoadTargetMeanTextBox(self):
          self.txtTarget = TextBox()
          self.txtTarget.Size = Size(100, 50)
          self.txtTarget.Location = Point(self.lblLCL.Left + 125, self.lblLCL.Bottom + 3)
  
      def Load_UCL_Label(self):
          self.lblUCL = Label()
          self.lblUCL.Text = 'Upper Control Limit'
          self.lblUCL.Size = Size(self.lblUCL.PreferredWidth, self.lblUCL.PreferredHeight)
          self.lblUCL.Location = Point(self.lblTarget.Left + 125, self.lblSep.Top + 15)
          self.lblUCL.BorderStyle = 0
  
      def Load_UCL_TextBox(self):
          self.txtUCL = TextBox()
          self.txtUCL.Size = Size(100, 50)
          self.txtUCL.Location = Point(self.lblTarget.Left + 125, self.lblLCL.Bottom + 3)
  
      def LoadCommentLabel(self):
          self.lblComment = Label()
          self.lblComment.Text = 'Comment'
          self.lblComment.Size = Size(self.lblUCL.PreferredWidth, self.lblUCL.PreferredHeight)
          self.lblComment.Location = Point(self.lblSep.Left, 350)
          self.lblComment.BorderStyle = 0
  
      def LoadCommentTextBox(self):
          self.txtComment = TextBox()
          self.txtComment.Multiline = True
          self.txtComment.Size = Size(500, 50)
          self.txtComment.Location = Point(self.lblSep.Left, self.lblComment.Bottom + 5)
  
      def LoadButton(self):
          self.button = Button()
          self.button.Size = Size(100, 30)
          self.button.Location = Point(self.lblSep.Left, self.txtComment.Bottom + 10)
          self.button.Text = &quot;Click Me!&quot;
  
      def PopulateDataGridView(self, selectcommand):
          bs = BindingSource()
  
          try:
              connectionstring = &quot;YourSQLServerConnectionString&quot;
  
              # Create a new data adapter based on the specified query.
              da = SqlDataAdapter(selectcommand, connectionstring)
  
              # Create a command builder to generate SQL update, insert, and delete commands based on selectCommand.
              cb = SqlCommandBuilder(da)
  
              # Populate a new data table and bind it to the BindingSource.
              dt = DataTable()
              Locale = CultureInfo.InvariantCulture
              da.Fill(dt)
              bs.DataSource = dt
  
              # Resize the DataGridView columns to fit the newly loaded content.
              self.dgvCCL.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)
  
              #String connectionString =
                  #&quot;Data Source=&lt;SQL Server&gt;;Initial Catalog=Northwind;&quot; +
                  #&quot;Integrated Security=True&quot;;
  
              #// Create a new data adapter based on the specified query.
              #dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
  
              #// Create a command builder to generate SQL update, insert, and
              #// delete commands based on selectCommand.
              #SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
  
              #// Populate a new data table and bind it to the BindingSource.
              #DataTable table = new DataTable
              #{
               #   Locale = CultureInfo.InvariantCulture
              #};
              #dataAdapter.Fill(table);
              #bindingSource1.DataSource = table;
  
              #// Resize the DataGridView columns to fit the newly loaded content.
              #dataGridView1.AutoResizeColumns(
                  #DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
  
          except SqlException:
              Messagebox.Show(&quot;Something happened.&quot;)
  
  Application.EnableVisualStyles()
  Application.SetCompatibleTextRenderingDefault(False)
  
  Application.Run(MyForm())
  </code></pre>