如题,谢谢

解决方案 »

  1.   

    <asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 264px; POSITION: absolute; TOP: 8px" runat="server"
    PageSize="3">
    <Columns>
    <asp:TemplateColumn>
    <EditItemTemplate>
    <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
    </EditItemTemplate>
    </asp:TemplateColumn>
    </Columns>

    </asp:datagrid>
      

  2.   

    windows form還是web form的?
      

  3.   

    <asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" DataKeyField="ProductID" OnUpdateCommand="DataGrid1_Update" 
     
         OnEditCommand="DataGrid1_Edit" OnCancelCommand="DataGrid1_Cancel" runat="server"> 
     
    <Columns> 
     
    <asp:TemplateColumn HeaderText="Discontinued"> 
     
         <ItemTemplate> 
     
         <asp:Label ID="lblDiscontinued" Text='<%#ShowVal(Convert.ToBoolean( DataBinder.Eval(Container.DataItem, "Discontinued").ToString()) )%>' Runat="server" /> 
     
         </ItemTemplate> 
      
         <EditItemTemplate> 
     
         <asp:Label runat="server" id="lblProductID" Visible="False" Text='<%# DataBinder.Eval(Container.DataItem, "ProductId") %>'/> 
     
         <asp:Label ID="lblEditDiscontinued" Text='<%#ShowVal(Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "Discontinued").ToString() ))%>' Runat="server" /> 
     
         <asp:DropDownList id="ddlDiscontinued" DataSource="<%# BindTheDiscontinued() %>" OnPreRender="SetDropDownIndex" DataTextField="Discontinued" DataValueField="Discontinued" runat="server" /> 
     
         </EditItemTemplate> 
     
    </asp:TemplateColumn> 
      
    <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" ItemStyle-Width="100px" HeaderText="Commands" /> 
     
    </Columns> 
     
    </asp:DataGrid>  
      

  4.   

    string strDiscontinued; 
     
    GetData obj; 
     
    string strSql; 
     
    string strConn; 
     
    DataSet ds; 
     
    SqlDataReader dr; 
     
    private void Page_Load(object sender, System.EventArgs e) 
     

     
    // Put user code to initialize the page here 
     
    strConn ="server=localhost;uid=sa;pwd=;database=northwind"; 
     
    if (!Page.IsPostBack ) 
     

     
    BindGrid(); 
     

     

       
    //To Bind the DataGrid 
     
    void BindGrid() 
     

     
         obj=new GetData (); 
     
         strSql = "Select productid, discontinued from Products"; 
     
         ds=obj.GetDataFromTable (strSql ,strConn); 
     
         DataGrid1.DataSource =ds; 
     
         DataGrid1.DataBind (); 
     

      
    //To display Yes/No for True/False 
     
    protected string ShowVal(bool blnval) 
     

     
         if (blnval==true) 
     
         { 
     
              return "Yes"; 
     
         } 
     
         else 
     
         { 
     
              return "No"; 
     
         } 
     

      
    //Bind the Data to the dropdownlist in the EditTemplate 
     
    protected SqlDataReader BindTheDiscontinued() 
     

     
         obj=new GetData (); 
     
         strSql ="SELECT distinct 'Discontinued' =" ; 
     
         strSql+=" CASE "; 
     
         strSql+=" WHEN Discontinued = 1 Then 'Yes'" ; 
     
         strSql+=" ELSE 'No'" ; 
     
         strSql+=" END " ; 
     
         strSql+=" From Products "; 
     
         dr=obj.GetSingleDataUsingReader (strSql ,strConn); 
     
         return dr; 
     

       
    //Set the Text of the Dropdownlist to the field value in Database 
      
    protected void SetDropDownIndex(Object sender ,System.EventArgs e ) 
     

     
         DropDownList ed ; 
     
         ed = (DropDownList) sender; 
     
         ed.SelectedIndex = ed.Items.IndexOf(ed.Items.FindByText(strDiscontinued)); 
     

       
    //For Edit Update Cancel 
     
    public void DataGrid1_Edit(Object sender, DataGridCommandEventArgs e) 
     

     
         strDiscontinued = ((Label )e.Item.FindControl("lblDiscontinued")).Text; 
     
         DataGrid1.EditItemIndex = (int)e.Item.ItemIndex; 
     
         BindGrid(); 
     

      
    public void DataGrid1_Update(Object sender, DataGridCommandEventArgs e) 
     

     
         DropDownList TempList ; 
     
         String TempValue ; 
     
         TempList = (DropDownList) e.Item.FindControl("ddlDiscontinued"); 
     
         TempValue = TempList.SelectedItem.Value; 
     
          //Place update code here 
     
         Response.Write (TempValue); 
     
         DataGrid1.EditItemIndex = -1; 
     
         BindGrid(); 
     

     
    public void DataGrid1_Cancel(Object sender, DataGridCommandEventArgs e) 
     

     
         DataGrid1.EditItemIndex = -1; 
     
         BindGrid(); 
     

        
    //Functions used in Class GetData.cs 
      
    SqlConnection mycn; 
     
    SqlDataAdapter myda; 
     
    SqlCommand mycmd; 
     
    DataSet ds; 
     
    String strConn; 
     
    SqlDataReader myReader; 
     
    public DataSet GetDataFromTable(string strSQL ,string strConnString) 
     

     
    try 
     

     
         strConn=strConnString; 
     
         mycn = new SqlConnection(strConn); 
     
         myda = new SqlDataAdapter (strSQL, mycn); 
     
         ds= new DataSet (); 
     
         myda.Fill (ds,"Table"); 
     
         return ds; 
     

     
    catch(Exception ex) 
     

     
         throw new Exception (ex.Message.ToString ()); 
     

     
    finally 
     

     
         mycn.Close (); 
     

     

      
    public SqlDataReader GetSingleDataUsingReader(string strSQL ,string strConnString) 
     

     
    try 
     

     
         strConn=strConnString; 
     
         mycn = new SqlConnection(strConn); 
     
         mycmd = new SqlCommand (strSQL, mycn); 
     
         mycn.Open (); 
     
         myReader=mycmd.ExecuteReader(CommandBehavior.CloseConnection ); 
     
         return myReader; 
     

     
    catch(Exception ex) 
     

     
         throw new Exception (ex.Message.ToString ()); 
     

     
    finally 
     

     
         //mycn.Close (); 
     

     

     
      

  5.   

    下面是winform的,添加一個ComBoBox到界面,設置visiable= false;
    單元格變化時,顯示ComboBox                   
    private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
    {
    int i = dataGrid1.CurrentCell.RowNumber;
    int j = dataGrid1.CurrentCell.ColumnNumber;
    int x = dataGrid1.Left;
    int y = dataGrid1.Top;
    Rectangle rect = dataGrid1.GetCellBounds(i, j);
    comboBox1.Left = x + rect.X;
    comboBox1.Top = y + rect.Y;
    comboBox1.Width = rect.Width;
    comboBox1.Height = rect.Height;
    comboBox1.Text = dataGrid1[i,j].ToString();
    comboBox1.Visible = true;
    }
      

  6.   

    http://www.syncfusion.com/faq/winforms/Files/DataGridTextBoxCombo.zip