0

I have a asp.net and c# app that displays a grid of records in the bottom portion of the screen and textboxes and buttons in the upper portion of the screen. One of the fields loaded in the grid is a byte field named RecordActive. I have a checkbox in the upper portion for this field. When a record in the grid is highlighted then the values from the record are shown in the upper portion textboxes and checkbox. However I am having trouble getting the value from the record and setting the checkbox. (which is done in Display_Values)

I have read several similar questions and answers in StackOverflow including:

Get the cell value of a GridView row How do I get the cell value from a datagridview using row index and column index in c#?

Which says to get the value this way:

String val = dgvHolidays.Rows[dgvHolidays.SelectedRow.RowIndex].Cells[3].Value.ToString();

But value in this code gets the following error:

CS1061: 'TableCell' does not contain a definition for 'Value' and no accessible extension Method 'Value' accepting a first argumentof type 'TableCell' could be found (are you missing a using directive or an assembly reference?)

Why?

Code:

namespace Productivity_ASPWeb
{
public partial class DM_Holidays : System.Web.UI.Page
{
    public SQLControl SQL = new SQLControl(GlobalVariables.strConnection);
    private int intSelectedRow = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)   // If not postback
        {
          //  txtSrchHoliday_Date.Text = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-2).ToShortDateString();
            if (GlobalVariables.updatetype == "")
                btnSearch_Click(null, EventArgs.Empty);
            dgvHolidays.SelectRow(GlobalVariables.oldrowindex);
            GlobalVariables.oldrowindex = dgvHolidays.SelectedRow.RowIndex;
            txtOldRowIndex.Value = dgvHolidays.SelectedRow.RowIndex.ToString();
            if (int.TryParse(txtOldRowIndex.Value.TrimStart(), out GlobalVariables.oldrowindex))
            {
                dgvHolidays.SelectRow(int.Parse(txtOldRowIndex.Value.TrimStart()));
            }
        }
        else
        {
            if (!Page.IsPostBack)   // If not postback
            {
                GlobalVariables.oldrowindex = dgvHolidays.SelectedRow.RowIndex;
                txtOldRowIndex.Value = dgvHolidays.SelectedRow.RowIndex.ToString();
                // if (GlobalVariables.updatetype == "")
                btnSearch_Click(null, EventArgs.Empty);
            }
        }
    }

    public void LoadGrid(string query = "")
    {
        if (string.IsNullOrEmpty(query))
        {
            // SQL.ExecuteQuery("Select * from Holidays where RecordActive = 'True';")
            SQL.ExecuteQuery("Select Holiday_Date ,Holiday_Description ,Week_Day ,RecordActive ,CreatedBy ,CreateDate ,ModifiedBy ,ModifiedDate from Holidays;");
        }
        else
        {
            SQL.ExecuteQuery(query);
        }

        if (SQL.HasException(true))
            return;
        dgvHolidays.DataSource = SQL.DBDS.Tables[0];
        dgvHolidays.DataBind();

        if (dgvHolidays.Rows.Count > 0)
        {
            //dgvHolidays.Rows[0].Selected = true;
            // if (dgvHolidays.SelectedValue != null)
            {
                // intSelectedRow = dgvHolidays.SelectedRow.RowIndex;
                if (int.TryParse(txtOldRowIndex.Value.TrimStart(), out GlobalVariables.oldrowindex))
                {
                    dgvHolidays.SelectRow(int.Parse(txtOldRowIndex.Value.TrimStart()));
                    dgvHolidays.SelectedIndex = int.Parse(txtOldRowIndex.Value.TrimStart());
                }
                else
                {
                    dgvHolidays.SelectRow(0);
                    dgvHolidays.SelectedIndex = 0;
                }
            }
        }

        // SQL.DBDA.UpdateCommand = New SqlClient.SqlCommandBuilder(SQL.DBDA).GetUpdateCommand
        if (dgvHolidays.Rows.Count > 0)
        {
            dgvHolidays_SelectionChanged(null, EventArgs.Empty);
            intSelectedRow = dgvHolidays.SelectedRow.RowIndex;
            intSelectedRow = dgvHolidays.SelectedIndex;

            if (dgvHolidays.Rows.Count > 0)
            {
                intSelectedRow = dgvHolidays.SelectedRow.RowIndex;
                if (int.TryParse(txtOldRowIndex.Value.TrimStart(), out GlobalVariables.oldrowindex))
                {

                    if (dgvHolidays.Rows.Count >= int.Parse(txtOldRowIndex.Value.TrimStart()))
                    {
                        dgvHolidays.SelectRow(int.Parse(txtOldRowIndex.Value.TrimStart()));
                        dgvHolidays.SelectedIndex = int.Parse(txtOldRowIndex.Value.TrimStart());
                        dgvHolidays.SelectedRow.Focus();
                        DisplayValues(); // Occurrence 1
                                         //  EnableButtons();
                    }
                }
            }
            else
            {
                intSelectedRow = dgvHolidays.Rows.Count;
                txtHoliday_Date.Text = "";
                txtHoliday_Description.Text = "";
                txtWeekday.Text = "";
                txtCreatedBy.Text = "";
                txtCreateDate.Text = "";
                txtModifiedBy.Text = "";
                txtModifiedDate.Text = "";
                chkbxRecordActive.Checked = false;
            }
        }
    }

    protected void dgvHolidays_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Text = "Holiday Date";
            e.Row.Cells[0].Width = 200;
             e.Row.Cells[1].Text = "Holiday_Description";
            e.Row.Cells[1].Width = 210;
            e.Row.Cells[2].Text = "Weekday";
            e.Row.Cells[2].Width = 80;
            e.Row.Cells[3].Text = "Record Active";
            e.Row.Cells[3].Width = 100;
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(dgvHolidays, "Select$" + e.Row.RowIndex);
            e.Row.ToolTip = "Click to select this row.";
        }
    }

    protected void dgvHolidays_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in dgvHolidays.Rows)
        {
            if (row.RowIndex == dgvHolidays.SelectedIndex)
            {
                row.BackColor = System.Drawing.Color.Aqua;
                row.ToolTip = string.Empty;
                GlobalVariables.oldrowindex = dgvHolidays.SelectedRow.RowIndex;
                txtOldRowIndex.Value = dgvHolidays.SelectedRow.RowIndex.ToString();
            }
            else
            {
                row.BackColor = System.Drawing.Color.LightGray;
                row.ToolTip = "Click to select this row.";
            }
        }
        DisplayValues();
    }

    private void dgvHolidays_SelectionChanged(object sender, EventArgs e)
    {
        DisplayValues();
    }

    private void DisplayValues()
    {
        if (dgvHolidays.Rows.Count > 0)
        {
            // 0 [Holiday_Date]
            if (dgvHolidays.Rows[dgvHolidays.SelectedRow.RowIndex].Cells[0].Text is object)
            {
                if (dgvHolidays.Rows[dgvHolidays.SelectedRow.RowIndex].Cells[0].Text == " ")
                    txtHoliday_Date.Text = ""; // Holidays.Holiday_Date
                else
                    txtHoliday_Date.Text = dgvHolidays.Rows[dgvHolidays.SelectedRow.RowIndex].Cells[0].Text.ToString(); // Holidays.Holiday_Date
            }
            // 3[RecordActive]
            if (dgvHolidays.Rows[dgvHolidays.SelectedRow.RowIndex].Cells[3].Text is object)
            {
                String val = dgvHolidays.Rows[dgvHolidays.SelectedRow.RowIndex].Cells[3].Value.ToString();
                if (getvalue(dgvHolidays.Rows[dgvHolidays.SelectedRow.RowIndex].Cells[3].Text))
                {
                    chkbxRecordActive.Checked = true; // Holidays.RecordActive
                }
                else
                {
                    chkbxRecordActive.Checked = false; // Holidays.RecordActive
                }
            }
       }
    } 

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SQL.AddParam("@Holiday_Date", txtSrchHoliday_Date.Text);
        SQL.AddParam("@Holiday_Description", txtSrchHoliday_Description.Text);
        SQL.AddParam("@Weekday", txtSrchWeekday.Text);
        LoadGrid(@"select Holiday_Date ,Holiday_Description ,Week_Day ,RecordActive ,CreatedBy ,CreateDate ,ModifiedBy ,ModifiedDate from Holidays where (@Holiday_Date = '' or Holiday_Date = @Holiday_Date) and (@Holiday_Description = '' or Holiday_Description like '%' + @Holiday_Description + '%') and (@Weekday = '' or Week_Day = @Weekday) order by Holiday_Date desc;");
    }
}

}

Darrell
  • 49
  • 8

1 Answers1

0

Your page load event has a !IsPostBack stub, and then with the else statement, you again have !IsPostack stub -- that code will never run. but, we can leave that mess for another day.

However, if you are letting the GV autogenerate columns, or not using a templated field? And it is a check box?

Then you have to use this to get the check box value, as the check box control is rendered as a child control of the cell.

Hence this:

  CheckBox myCheck = (CheckBox)MyGridRow.Cells[4].Controls(0);
  myCheck.Checked   <--- that gets you the true/false value
Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • Tried this: chkbxRecordActive = (CheckBox)dgvHolidays.Rows[dgvHolidays.SelectedRow.RowIndex].Cells[3].Controls(0); and I get error: CS1955 Non-invocable member 'Control.Controls' cannot be used like a method. – Darrell Aug 26 '22 at 13:28