Для нестатического поля, метода или свойства требуется ссылка на объект - C# (189830)
Формулировка задачи:
Замучился уже. Кто может помочь? Есть 2 datagrid заполняемых динамически (по регистрации карточек в микрофонах) Нужно то всего лишь сравнить если человек зарегистрировал карточку в микрофоне но не нажал кнопку добавить его в datagrid на другой форме. Пришел на ум такой метод, но пишет, что требуется ссылка на объект. ПОМОГИТЕ НОВИЧКУ ПОЖАЛУЙСТА))
Форма из которой должно считать
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
namespace DcnSwSmd
{
/// <summary>
/// Form to display the Attendance Registration details.
/// </summary>
public partial class AttendanceRegistrationForm : Form
{
#region Methods
/// <summary>
/// Contructor.
/// </summary>
public AttendanceRegistrationForm()
{
InitializeComponent();
miFullScreen.Checked = true;
miFullScreen.Text = "Full Screen";
miFullScreen.Click += new EventHandler(OnFullScreenClick);
cmIndvResults.MenuItems.Add(miFullScreen);
this.ContextMenu = cmIndvResults;
this.headerControl.lblTimeLimit.Visible = false;
}
/// <summary>
/// Function to load the required data to display.
/// </summary>
/// <param name="participantinfo"></param>
public delegate void UpdateAttendanceRegistrationData(SMDParticipantData participantInfo);
public void LoadAttendanceRegistratioData(SMDParticipantData participantinfo)
{
if (this.InvokeRequired)
{
UpdateAttendanceRegistrationData obj = new UpdateAttendanceRegistrationData(LoadAttendanceRegistratioData);
this.Invoke(obj, participantinfo);
}
else
{
if (participantinfo != null)
{
DataTable participantInformation = new DataTable();
DateTime.Now.ToShortDateString().ToString(CultureInfo.InvariantCulture);
this.headerControl.lblTime.Text = DateTime.Now.ToShortTimeString().ToString(CultureInfo.InvariantCulture);
participantInformation = participantinfo.ParticipantApptendanceDetails.Copy();
this.headerControl.lblTimeLimit.Visible = false;
this.lblPresent.Text = /*Properties.Resources.AttendanceRegistrationTxtPresent*/ "Зарегестрировано:" + " " + participantinfo.ParticipantApptendanceDetails.Select("Attendance_Status = 'true'").Length.ToString();
double kv_pres = Convert.ToDouble(participantinfo.ParticipantApptendanceDetails.Select("Attendance_Status = 'true'").Length.ToString());
double kv_abs = Convert.ToDouble(participantinfo.ParticipantApptendanceDetails.Select("Attendance_Status = 'false'").Length.ToString());
this.lblAbsent.Text = "Присутствуют:" + " " + Convert.ToString(kv_pres + kv_abs);
}
}
this.dgvAttendanceRegistration.DataSource = participantInformation;
while (dgvAttendanceRegistration.Rows.Count > 10)
{
dgvAttendanceRegistration.Rows.RemoveAt(10);
}
foreach (DataGridViewRow dgvRow in dgvAttendanceRegistration.Rows)
{
if (dgvRow.Cells["Attendance_Status"].Value.ToString() == "true")
{
dgvRow.DefaultCellStyle.ForeColor = Color.Green;
}
else if (dgvRow.Cells["Attendance_Status"].Value.ToString() == "false")
{
dgvRow.DefaultCellStyle.ForeColor = Color.Red;
}
}
if (this.dgvAttendanceRegistration.ColumnCount > 0)
{
if (this.dgvAttendanceRegistration.Columns.Contains("Participant_Id"))
{
this.dgvAttendanceRegistration.Columns["Participant_Id"].Visible = false;
}
if (this.dgvAttendanceRegistration.Columns.Contains("Attendance_Status"))
{
this.dgvAttendanceRegistration.Columns["Attendance_Status"].Visible = false;
}
}
this.dgvAttendanceRegistration.Refresh();
}
}
}
public delegate void UpdateHeaderDateAndTimeDelegate();
public void UpdateHeaderDateAndTime()
{
if (this.InvokeRequired)
{
UpdateHeaderDateAndTimeDelegate obj = new UpdateHeaderDateAndTimeDelegate(UpdateHeaderDateAndTime);
this.Invoke(obj);
}
else
{
this.headerControl.lblTime.Text = DateTime.Now.ToShortTimeString().ToString(CultureInfo.InvariantCulture);
}
}
private void OnFullScreenClick(object sender, EventArgs e)
{
if (this.miFullScreen.Checked)
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.miFullScreen.Checked = false;
}
else if (!this.miFullScreen.Checked)
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.miFullScreen.Checked = true;
}
}
private void dgvAttendanceRegistration_SelectionChanged(object sender, EventArgs e)
{
if (dgvAttendanceRegistration.SelectedRows.Count > 0)
{
dgvAttendanceRegistration.ClearSelection();
}
}
#endregion
#region Fields
ContextMenu cmIndvResults = new System.Windows.Forms.ContextMenu();
MenuItem miFullScreen = new MenuItem();
#endregion
private void AttendanceRegistrationForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
AttendanceRegistrationForm.ActiveForm.Close();
}
}
internal static bool Equals()
{
throw new NotImplementedException();
}
}
}Решение задачи: «Для нестатического поля, метода или свойства требуется ссылка на объект»
textual
Листинг программы
private void HideLabels()
{
this.totalLabel.Visible = false;
this.totalValuelabel.Visible = false;
this.notVotedLabel.Visible = false;
this.notVotedValueLabel.Visible = false;
}
private delegate void UpdateStaticDataDelegate(SMDVotingData votingIndvResults);
public void UpdateStaticData(SMDVotingData votingIndvResults)
{
if (this.InvokeRequired)
{
UpdateStaticDataDelegate obj = new UpdateStaticDataDelegate(UpdateStaticData);
this.Invoke(obj, votingIndvResults);
}
else
{
if (votingIndvResults != null)
{
ResetUserInterface();
if (votingIndvResults.VotingData != null)
{
if (votingIndvResults.VotingData.RemainingVotingTime <= -1)
{
this.headerControl.lblTimeLimit.Text = Properties.Resources.VotingResultsTxtTimeLimit + ": NA";
}
else
{
TimeSpan votingTimeLimit;
votingTimeLimit = new TimeSpan(votingIndvResults.VotingData.RemainingVotingTime * TimeSpan.TicksPerSecond);
this.headerControl.lblTimeLimit.Text = Properties.Resources.VotingResultsTxtTimeLimit + ": " + string.Format(System.Globalization.CultureInfo.InvariantCulture, RemainingvotingTimeFormatString, Math.Abs(votingTimeLimit.Minutes), Math.Abs(votingTimeLimit.Seconds));
}
}
HideLabels();
if (votingIndvResults.Answers != null)
{
foreach (AnswerContainer answer in votingIndvResults.Answers)
{
this.totalLabel.Visible = true;
this.totalValuelabel.Visible = true;
this.notVotedLabel.Visible = true;
this.notVotedValueLabel.Visible = true;
ResetUserInterface();
}
}
}
}
}
#region Interim Results
public delegate void AddorUpdateInterimResultsDelegate(SMDVotingData votingIndvResults);
public void AddOrUpdateInterimResults(SMDVotingData votingIndvResults)
{
if (this.InvokeRequired)
{
AddorUpdateInterimResultsDelegate obj = new AddorUpdateInterimResultsDelegate(AddOrUpdateInterimResults);
this.Invoke(obj, votingIndvResults);
}
else
{
try
{
if (votingIndvResults != null)
{
if (votingIndvResults.IndividualResults != null && votingIndvResults.IndividualResults.Count > 0)
{
int ansId = 0;
IndvResults indvResult = new IndvResults();
string participantId = String.Empty;
string participantName = String.Empty;
string participantGroup = String.Empty;
string answer = String.Empty;
for (int iCount = 0; iCount < votingIndvResults.IndividualResults.Count && iCount < 10; iCount++)
{
ansId = votingIndvResults.IndividualResults[iCount].AnswerId;
int ansIndex = votingIndvResults.Answers.FindIndex(delegate(AnswerContainer ansCont) { return ansCont.Id == ansId; });
if (ansIndex > -1)
{
indvResult.Answer = votingIndvResults.Answers[ansIndex].AnswerText;
indvResultsDataGridView.Rows[ansId].Visible = false;
}
else
{
indvResult.Answer = "Not Voted";
}
indvResult.ParticipantId = votingIndvResults.IndividualResults[iCount].Participant.Id.ToString(CultureInfo.InvariantCulture);
indvResult.ParticipantName = votingIndvResults.IndividualResults[iCount].Participant.ParticipantData.FirstName + " " + votingIndvResults.IndividualResults[iCount].Participant.ParticipantData.LastName;
indvResult.ParticipantGroup = votingIndvResults.IndividualResults[iCount].Participant.Group.Name;
if (indvResultsDataSource.Count > 0 )
{
var modifyIndvResultList = indvResultsDataSource.Where(x => x.ParticipantId == indvResult.ParticipantId).ToList();
if (modifyIndvResultList.Count > 0)
{
foreach (var iResult in modifyIndvResultList)
{
indvResultsDataSource.Remove(iResult);
}
indvResultsDataSource.Add(indvResult);
}
else
{
indvResultsDataSource.Add(indvResult);
}
}
else if (indvResultsDataSource.Count == 0)
{
indvResultsDataSource.Add(indvResult);
}
}
}
}
indvResultsBindingSource.DataSource = indvResultsDataSource;
UpdateGridViewImage();
UpdateTotalResults(votingIndvResults);
}
catch (Exception)
{
}
}
}
private void indvResultsDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
UpdateGridViewImage();
}
#endregion
private delegate void UpdateCaptionOfControls(Control c, string str);
private void UpdateCaptionOfControl(Control cntr, string value)
{
if (!IsDisposed)
{
if (cntr.InvokeRequired)
{
Invoke(new UpdateCaptionOfControls(UpdateCaptionOfControl), cntr, value);
}
else
{
cntr.Text = value;
}
}
}
private delegate void UpdateDataSource(DataGridView ctrl, DataTable source);
private void UpdateDataSourceOfGrid(DataGridView ctrl, DataTable source)
{
if (!IsDisposed)
{
if (ctrl.InvokeRequired)
{
Invoke(new UpdateDataSource(UpdateDataSourceOfGrid),indvResultsDataGridView, source);
}
else
{
ctrl.DataSource = source;
}
}
}
private delegate void UpdateDatgridView(DataGridView ctrl);
private void UpdateDataGird(DataGridView ctrl)
{
if(!IsDisposed)
{
if(ctrl.InvokeRequired)
{
Invoke(new UpdateDatgridView(UpdateDataGird), indvResultsDataGridView);
}
else
{
ctrl = indvResultsDataGridView;
}
}
}
private void indvResultsDataGridView_SelectionChanged(object sender, EventArgs e)
{
if (indvResultsDataGridView.SelectedRows.Count > 0)
{
indvResultsDataGridView.ClearSelection();
}
}
public delegate void UpdateVotingRemainingTimeDelegate(int remainingVotingTime);
public void UpdateRemainingVotingTime(int remainingVotingTime)
{
if (this.InvokeRequired)
{
UpdateVotingRemainingTimeDelegate obj = new UpdateVotingRemainingTimeDelegate(UpdateRemainingVotingTime);
this.Invoke(obj,remainingVotingTime);
}
else
{
// this.headerControl.lblDate.Text = DateTime.Now.ToShortDateString().ToString(CultureInfo.InvariantCulture);
this.headerControl.lblTime.Text = DateTime.Now.ToShortTimeString().ToString(CultureInfo.InvariantCulture);
if (votingResultsData != null)
{
TimeSpan votingTimeLimit;
if (votingResultsData.VotingData != null)
{
if (votingResultsData.VotingData.RemainingVotingTime <= -1)
{
this.headerControl.lblTimeLimit.Text = Properties.Resources.VotingResultsTxtTimeLimit + ": NA";
}
else
{
votingResultsData.VotingData.RemainingVotingTime = remainingVotingTime;
votingTimeLimit = new TimeSpan(votingResultsData.VotingData.RemainingVotingTime * TimeSpan.TicksPerSecond);
this.headerControl.lblTimeLimit.Text = Properties.Resources.VotingResultsTxtTimeLimit + ": " + string.Format(System.Globalization.CultureInfo.InvariantCulture, RemainingvotingTimeFormatString, Math.Abs(votingTimeLimit.Minutes), Math.Abs(votingTimeLimit.Seconds));
}
}
}
}
}
public delegate void UpdateHeaderDateAndTimeDelegate();
public void UpdateHeaderDateAndTime()
{
if (this.InvokeRequired)
{
UpdateHeaderDateAndTimeDelegate obj = new UpdateHeaderDateAndTimeDelegate(UpdateHeaderDateAndTime);
this.Invoke(obj);
}
else
{
this.headerControl.lblTime.Text = DateTime.Now.ToShortTimeString().ToString(CultureInfo.InvariantCulture);
}
}
#endregion
#region Properties
/// <summary>
/// Object to hold the voting data.
/// </summary>
public SMDVotingData VotingIndvResults
{
get
{
return votingResultsData;
}
set
{
votingResultsData = value;
}
}
public DataTable DtIntermediateIndvResults
{
get
{
return dtInterimIndvResults;
}
set
{
dtInterimIndvResults = value;
}
}
public BindingList<IndvResults> IndvResultsDataSource
{
get
{
return indvResultsDataSource;
}
set
{
IndvResultsDataSource = value;
}
}
#endregion
#region Fields
const string RemainingvotingTimeFormatString = "{0:00}:{1:00}";
private BindingList<IndvResults> indvResultsDataSource = new BindingList<IndvResults>();
/// <summary>
/// A datatable to hold the interim individual Voting results.
/// </summary>
DataTable dtInterimIndvResults;
SMDVotingData votingResultsData;
/// <summary>
/// A datatable to hold the individual Voting results.
/// </summary>
ContextMenu cmIndvResults = new System.Windows.Forms.ContextMenu();
MenuItem miFullScreen = new MenuItem();
#endregion
private void IndividualResults_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
IndividualResults.ActiveForm.Close();
}
}
}
public class IndvResults : INotifyPropertyChanged
{
string participant_Id;
string answer;
string participant_Name;
string participant_Group;
#region Properties
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
public string ParticipantId
{
get
{
return participant_Id;
}
set
{
participant_Id = value;
this.NotifyPropertyChanged("ParticipantId");
}
}
public string Answer
{
get
{
return answer;
}
set
{
answer = value;
this.NotifyPropertyChanged("Answer");
}
}
public string ParticipantName
{
get
{
return participant_Name;
}
set
{
participant_Name = value;
this.NotifyPropertyChanged("ParticipantName");
}
}
public string ParticipantGroup
{
get
{
return participant_Group;
}
set
{
participant_Group = value;
this.NotifyPropertyChanged("ParticipantGroup");
}
}
#endregion
}
}