C# textbox button datagridview 实现下拉框,并在选中时可以对其他控件赋值

在C# WinForms中,通过DataGridView的下拉框选择值并联动其他控件,可按照以下步骤实现:

实现步骤

  1. 界面设计
    在窗体中添加:

    • TextBox 控件(命名 txtDisplay
    • Button 控件(命名 btnAction
    • DataGridView 控件(命名 dataGridView1
  2. 配置DataGridView列
    DataGridView 添加 DataGridViewComboBoxColumn 作为下拉框列:

    // 创建下拉框列
    DataGridViewComboBoxColumn comboColumn = new DataGridViewComboBoxColumn();
    comboColumn.HeaderText = "选项";
    comboColumn.Name = "Options";
    comboColumn.Items.AddRange("选项A", "选项B", "选项C"); // 下拉项
    dataGridView1.Columns.Add(comboColumn);
    
  3. 处理下拉框选择事件
    DataGridViewEditingControlShowing 事件中订阅下拉框的 SelectedIndexChanged 事件:

    private void dataGridView1_EditingControlShowing(object sender, 
        DataGridViewEditingControlShowingEventArgs e)
    {
        if (dataGridView1.CurrentCell.ColumnIndex == 0) // 下拉框列索引
        {
            ComboBox comboBox = e.Control as ComboBox;
            if (comboBox != null)
            {
                comboBox.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
                comboBox.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
            }
        }
    }
    
  4. 实现联动赋值逻辑
    当选择下拉项时,更新TextBox和Button:

    private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        ComboBox comboBox = sender as ComboBox;
        if (comboBox != null && dataGridView1.CurrentRow != null)
        {
            // 获取选中值
            string selectedValue = comboBox.SelectedItem.ToString();
            
            // 更新TextBox
            txtDisplay.Text = $"已选择: {selectedValue}";
            
            // 更新Button属性
            btnAction.Text = $"执行{selectedValue}";
            btnAction.BackColor = selectedValue == "选项A" ? Color.LightGreen : Color.LightCoral;
        }
    }
    
  5. 初始化数据源
    在窗体加载时添加测试数据:

    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.Rows.Add("选项A");
        dataGridView1.Rows.Add("选项B");
        dataGridView1.Rows.Add("选项C");
    }
    

关键说明

  1. 事件处理机制

    • EditingControlShowing 在用户开始编辑单元格时触发
    • SelectedIndexChanged 实时响应下拉框选择变化
  2. 联动效果

    • TextBox 显示当前选择项
    • Button 的文本和背景色随选择动态变化
    • 效果示例:选择"选项A"时按钮变为绿色,选择"选项B"时变为红色
  3. 注意事项

    • 使用 comboBox.SelectedIndexChanged -= ... 避免重复订阅事件
    • 检查 CurrentRow 防止空引用异常
    • 下拉项数据可通过数据库/集合动态绑定

完整代码需在窗体设计器中关联事件:

// 在Form1.Designer.cs中确保事件绑定
this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(this.dataGridView1_EditingControlShowing);
this.Load += new System.EventHandler(this.Form1_Load);
<asp:TemplateColumn HeaderText="Plant" HeaderStyle-HorizontalAlign="center"> <ItemStyle CssClass="dxgv"></ItemStyle> <ItemTemplate> <asp:Label ID="lb_week_p" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.PLANTKEY") %>'> </asp:Label> </ItemTemplate> <FooterTemplate> <font face="宋体"></font> <asp:DropDownList ID="ddl_week_p" runat="server" Width="100px" AutoPostBack="true"> <asp:ListItem Selected="True" Value="">----</asp:ListItem> </asp:DropDownList> </FooterTemplate> <EditItemTemplate> <asp:DropDownList ID="ddl_e_week_p" ToolTip='<%# DataBinder.Eval(Container, "DataItem.PLANTKey") %>' OnSelectedIndexChanged="ddl_e_week_p_SelectedIndexChanged" AutoPostBack="true"runat="server" Width="100px"> </asp:DropDownList> <asp:Label ID="lb_week_p_edit" Visible="false" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.PLANTKEY") %>'></asp:Label> </EditItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="Storage Location" HeaderStyle-HorizontalAlign="center"> <ItemStyle CssClass="dxgv"></ItemStyle> <ItemTemplate> <asp:Label ID="lb_week_s" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.STORAGE_LOCATIONKEY") %>'> </asp:Label> </ItemTemplate> <FooterTemplate> <font face="宋体"></font> <asp:DropDownList ID="ddl_week_s" runat="server" Width="90px"> <asp:ListItem Selected="True" Value="">----</asp:ListItem> </asp:DropDownList> </FooterTemplate> <EditItemTemplate> <asp:DropDownList ID="ddl_e_week_s" ToolTip='<%# DataBinder.Eval(Container, "DataItem.STORAGE_LOCATIONKey") %>' runat="server" Width="90px"> </asp:DropDownList> <asp:Label ID="lb_week_s_edit" Visible="false" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.STORAGE_LOCATIONKEY") %>'></asp:Label> </EditItemTemplate> </asp:TemplateColumn> protected void ddl_week_p_SelectedIndexChanged(object sender, EventArgs e) { DropDownList item = (DropDownList)((Control)sender).Parent.Parent.FindControl("ddl_week_s"); item.DataTextField = "txt";
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值