// code assumes you have a DataSet named myDataSet, a table named "EastCoastSales" and a DataGrid myDataGrid 
 
//STEP 1: Create a DataTable style object and set properties if required. 
 
     DataGridTableStyle ts1 = new DataGridTableStyle(); 
  
     //specify the table from dataset (required step) 
 
     ts1.MappingName = "EastCoastSales"; 
 
      
 
     // Set other properties (optional step) 
 
     ts1.AlternatingBackColor = Color.LightBlue; 
  
//STEP 2: Create a string column and add it to the tablestyle 
 
     DataGridColumnStyle TextCol = new DataGridTextBoxColumn(); 
 
     TextCol.MappingName = "custName"; //from dataset table 
 
     TextCol.HeaderText = "Customer Name"; 
 
     TextCol.Width = 250; 
 
     ts1.GridColumnStyles.Add(TextCol); 
  
//STEP 3: Create an int column style and add it to the tablestyle 
 
     //this requires setting the format for the column through its property descriptor 
 
     PropertyDescriptorCollection pdc = this.BindingContext 
 
           [myDataSet, "EastCoastSales"].GetItemProperties(); 
  
     //now created a formated column using the pdc 
 
     DataGridDigitsTextBoxColumn csIDInt = 
 
           new DataGridDigitsTextBoxColumn(pdc["CustID"], "i", true); 
 
     csIDInt.MappingName = "CustID"; 
 
     csIDInt.HeaderText = "CustID"; 
 
     csIDInt.Width = 100; 
 
     ts1.GridColumnStyles.Add(csIDInt); 
  
//STEP 4: Add the checkbox 
 
     DataGridColumnStyle boolCol = new DataGridBoolColumn(); 
 
     boolCol.MappingName = "Current"; 
 
     boolCol.HeaderText = "Info Current"; 
  
//uncomment this line to get a two-state checkbox 
 
     //((DataGridBoolColumn)boolCol).AllowNull = false; 
  
     boolCol.Width = 150; 
 
     ts1.GridColumnStyles.Add(boolCol); 
  
//STEP 5: Add the tablestyle to your datagrid's tablestlye collection 
 
     myDataGrid.TableStyles.Add(ts1);
 

解决方案 »

  1.   

    如何判断选中:
    // Create a connection 
     
    SqlConnection connection = new SqlConnection(this.GetConnectionString()); 
      
    // Create a data adapter. Think of the data adapter as an object that knows how to get the data from the 
     
    // data source into a dataset 
     
    SqlDataAdapter dataAdapter = new SqlDataAdapter(this.GetCommandText(), connection); 
      
    // fill the dataset using the data adapter 
     
    DataSet dataSet = new DataSet("Customers"); 
     
    dataAdapter.Fill(this.dataSet, "Customers"); 
      
    // bind to the grid 
     
    grid.DataSource = this.dataSet; // the big picture      
     
    grid.DataMember = "Customers"; // the specific table that we want to bind to 
      
    // The connection text looks like this 
     
    // If your SQL server is running on the default port, you can remove the port attribute. 
     
    private string GetConnectionString() 
     
              { 
     
                    
      
    string server = "your_server_name"; 
     
                   string serverPort = "port_address"; 
     
                   string catalog = "NorthWind"; 
     
                   string password = "user_pass"; 
     
                   string userId = "user_name"; 
      
                   string connectionString = "data source={0},{1};initial catalog={2};" + 
     
                                                      "password={3}; user id={4}; packet size=4096"; 
     
                    
     
                   return string.Format(connectionString, 
     
                        server, serverPort, catalog, password, userId); 
     
              } 
        
    // The command text looks like this 
      
    private string GetCommandText() 
     
              { 
     
                   string commandText = "Select * from customers"; 
     
                   return commandText; 
     
              } 
     
      

  2.   

    sorry,the following is right
    5.24 How do I determine whether a checkbox in my datagrid is checked or not?     If the column is a boolean column, you can just cast the object returned by the indexer to a bool and use it. 
     
    if((bool)dataGridTopics[row, column]) 
     
         MessageBox.Show("I am true"); 
     
    else 
     
         MessageBox.Show("I am false");
     
      

  3.   

    See this:
    How can I put a checkbox in a column of my DataGrid?
    http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q754q
      

  4.   

    http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q754q
    5.11
      

  5.   

    在页面中加入这样的模板列:
    <asp:TemplateColumn HeaderText="选中">
      <ItemTemplate>
         <input type="checkbox" id="check1">
      </ItemTemplate>
    </asp:TemplateColumn>
    访问方法是DataGrid.Items[行号].Cells[列号].Controls[控件号]
    多调试几次,有时号码不对,就出现Index超出范围的异常。