Click here to Skip to main content
16,012,082 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

I have a Grid with various columns. For eg. Grid havng a column with status. I want to enable / disable the buttons(edit button, cancel button) as per the status (that particular row selected). their are other columns in grid as well.
XML
<Button x:Name="btnEdit" Content="Edit" Style="{StaticResource GreyButtonStyle}" IsEnabled="{Binding EditEnable}" >
          <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
              <cmd:EventToCommand Command="{Binding Path=EditCustomerOrdersCommand}" PassEventArgsToCommand="True" />
            </i:EventTrigger>
          </i:Interaction.Triggers>
        </Button> <DataGrid:GridViewDataColumn DataMemberBinding="{Binding Status}" Header="Status" TextWrapping="NoWrap" Width="*" />

I' using MVVM Pattern for my development.

Please help me.

Thanks in Advance.
Posted
Updated 6-Jun-11 21:50pm
v3

1 solution

Hi, i would use the RowDataBound method of the grid. Analize your data and then disable the control as you wish.

Grid Code:
<asp:gridview id="GridView1" runat="server" xmlns:asp="#unknown">
           onrowdatabound="GridView1_RowDataBound">
           <columns>
               <asp:boundfield datafield="Name" headertext="Name" />
               <asp:commandfield showeditbutton="True" />
           </columns>
       </asp:gridview>


Code Behind
protected void Page_Load(object sender, EventArgs e)
        {
//Fill th DataSource

            DataTable tbl = new DataTable("data");
            tbl.Columns.Add("Name");
            DataRow row = tbl.NewRow();
            row["Name"] = "Luis";
            DataRow row2 = tbl.NewRow();
            row2["Name"] = "Carl";
            
            tbl.Rows.Add(row);
            tbl.Rows.Add(row2);

//Bind the grid
            this.GridView1.DataSource = tbl;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
//Disable the control
                if (((System.Data.DataRowView)(e.Row.DataItem)).Row[0].Equals("Luis"))
                {
                    ((WebControl)((System.Web.UI.WebControls.TableRow)(e.Row)).Cells[1].Controls[0]).Enabled = false;
                    
                }
            }
        }



I hope it help you. Regards.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900