I'm using a database called babaEntities in C# and have the following code:
db = new babaEntities();
comboBox1.DataSource = from x in db.Tipusok
orderby x.TipusNev
select new { x.TipusNev, x.TipusID };
comboBox1.DisplayMember = "TipusNev";
comboBox1.ValueMember = "TipusID";
TipusID is integer, TipusNev is nchar
private void frissites()
{
var termekek = from x in db.Termekek
where x.TermekNev.StartsWith(textBox1.Text)
where x.TipusID == (int?)comboBox1.SelectedValue
select new
{
x.TermekNev,
x.EgysegAr,
x.Raktaron,
x.Egysegek.EgysegNev
};
termekekBindingSource.DataSource = termekek;
}
TermekNev is nchar, EgysegAr is int, Raktaron is int, Egysegnev is nchar. (ternekek is the datasource for a datagridview)
Whenever I try to run the frissites() method in ComboBoxSelectedIndexChanged event like this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
frissites();
}
it gives the following error, "InvalidCastExpection was unhandled by user code - Specified cast is not valid", and it doesn't run - but if I run it in the TextBox1_TextChanged method, it's completely fine, even if I just run the .exe file. I have been at this for hours. Can anyone help? I hope my problem is understandable.
(Basically, I want to be able to narrow the data with the textbox and have it only search for certain types (=tipus) from a combobox.)
I can spot one suspicious cast here:
where x.TipusID == (int?)comboBox1.SelectedValue
it may be valid, but let's make sure that this is NOT the culprit by changing it to something like:
where x.TipusID == GetTipusID(comboBox1.SelectedValue);
...
private int GetTipusID(object selected)
{
if (selected == null) return -1;
int value;
if(Int32.TryParse(selected.ToString(), out value)) return value;
else return -1;
}
Related
I'm defining the MultiSelectListPreference in my axml file like this:
<MultiSelectListPreference
android:title="Title"
android:summary="Sum"
android:key="dutyMarks"
android:entries="#array/array_marks"
android:entryValues="#array/array_marksValues">
</MultiSelectListPreference>
How can check / read the checked boxes in code?
I tried to get the checked values via the PreferenceChange event:
The checked values appear there, but I have no idea how to get them...
This was also my first thought, but e.NewValue doesn't contain a public definition for 'GetEnumerator'.
We can simply cast e.NewValue from object to IEnumerable in our code, for example:
private void PreferenceChange(object sender, Preference.PreferenceChangeEventArgs e)
{
var selections = e.NewValue as IEnumerable;
if (selections != null)
{
foreach (var selection in selections)
{
Console.WriteLine(selection);
}
}
}
since e.NewValue is a HashSet, you should be able to iterate through it
foreach (string x in e.NewValue) {
// do whatever you need to do with each value x here
}
If someone stumbles across this question, here is how i solved it:
Read (the async part is pretty ugly, but I don't know how to add a 'preferenceChangeD' event)
private async void preferenceChange(object sender, Preference.PreferenceChangeEventArgs e)
{
await System.Threading.Tasks.Task.Delay(100);
List<string> sList = new List<string>();
ICollection<string> selectedValues = mslp_dutyMarks.Values;
foreach(string x in selectedValues)
sList.Add(x);
}
Write
mslp_dutyMarks.Values = new List<string> { "A", "B", "C" };
I have a small problem regarding C# and WindowsForms.
I'm trying to get string SelectedItemName = combobox2.SelectedItem.ToString(); this variable in another class. For example I have this in my Form1.cs Class.
public void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
FileIniDataParser fileParser = new FileIniDataParser();
IniData data = fileParser.ReadFile("config.ini");
IniProgram classReference = new IniProgram();
string SelectedItemName = (string)comboBox2.SelectedItem.ToString();
// string _SelectedItemName = (string)comboBox2.SelectedText;
Console.WriteLine(SelectedItemName);
if (comboBox2.SelectedIndex > -1)
{
testvariabel2.GetSessionName();
}
}
And than my other Class CTestRack.cs looks like this:
if (_form1Object.comboBox2.SelectedIndex.ToString() != null)
{
string SelectedItemName = _form1Object.comboBox2.SelectedItem.ToString();
System.Threading.Thread.Sleep(1000);
if (newDictionary.ContainsKey(SelectedItemName))
Now I've tried getting and setting the variable in the Form1 class but I was just getting Loop errors, now with this method I'm getting a NULLReferenceException.
By the way I was already looking into several related posts here in SO but didn't found my answer yet.
My question is just how do I get the active Text from the Combobox in my other Class as a String?
Ensure that _form1Object, is indeed set to the instance of form you want to access... It's hard to tell from above code if correct initialization of _form1Object is the problem here.
Assuming _form1Object, is correctly initialized, one bug in above code is that ComboBox.SelectedIndex property is an int.
See http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex(v=vs.110).aspx
It doesn't matter if there is SelectedItem or not, comboBox2.SelectedIndex.ToString() != null will always be true, but comboBox2.SelectedItem could still be null, and hence comboBox2.SelectedItem.ToString() would fail with NullReferenceException.
It's possible that comboBox2 doesn't have SelectedItem, you can check it
either by
comboBox2.SelectedItem != null
Or by
comboBox2.SelectedIndex >= 0
Something like that
if (comboBox2.SelectedItem != null) {
string SelectedItemName = comboBox2.SelectedItem.ToString();
Console.WriteLine(SelectedItemName);
testvariabel2.GetSessionName();
}
else {
// No selected item in the ComboBox
}
...
if (_form1Object != null)
if (_form1Object.comboBox2.SelectedIndex >= 0) {
string SelectedItemName = _form1Object.comboBox2.SelectedItem.ToString();
System.Threading.Thread.Sleep(1000);
if (newDictionary.ContainsKey(SelectedItemName))
...
}
I was trying to output a single List variable that retrieves data from database via a CodeBehind code to a text field in ASPX:
<asp:TextBox ID="TBCluster" runat="server" CssClass="textbox"></asp:TextBox>
C# is used and the code goes something like this:
public List<shuffleDataList> pullShuffledData(SqlDataReader rdr)
{
List<shuffleDataList> callList = new List<shuffleDataList>();
if (rdr != null)
{
if (rdr.HasRows)
{
while (rdr.Read())
{
callList.Add(new shuffleDataList()
{
cluster = rdr.IsDBNull(5) ? null : rdr.GetString(5),
});
}
}
else
{
Response.Write("<script>alert('the data is null')</script>");
return null;
}
}
return callList;
}
The retrieval of cluster field will occur after a user clicks on a particular button and so my passing the variable goes into like the following:
protected void shuffle_Click(object sender, EventArgs e)
{
getdata();
TBCluster.Text = new shuffleDataList().cluster;
}
However nothing is displayed on the textfield. On the same query, I can display the data on a datagrid view but not on a text field? Any ideas why is this occuring?
Thank you
You don't actually appear to be calling your function. Also, since a Text property is generally a string.. you won't be able to assign a list to it (with any meaningful result). Therefore, I will make a heap of assumptions about your code.. and give you this:
var list = pullShuffledData(someReaderHere);
if (list != null)
TBCluster.Text = string.Join(", ", list.Select(x => x.cluster));
I have finally solved my issue:
string cluster = string.Empty;
DataSet ds = new DataSet();
List<shuffleDataList> list = pullShuffledData(rdr);
foreach(shuffleDataList item in list)
{
cluster = item.cluster;
}
TBCluster.Text = cluster;
It was supposed to be displayed early on but this gridview seems to be clearing the value of pullShuffledData once assigned
//gridviewShuffle.DataSource = pullShuffledData(rdr);
//gridviewShuffle.DataBind();
after commenting out, the cluster value finally appeared on text box. thanks
I'm trying to implement a search as you type (like in iTunes). I am using an ObjectListView. Further, I have a textbox that is used to do the search as shown below:
private void textBoxSearch_TextChanged(object sender, EventArgs e)
{
string txt = textBoxSearch.Text;
TextMatchFilter filter = null;
if (!String.IsNullOrEmpty(txt))
{
filter = TextMatchFilter.Contains(myObjectListView, txt);
}
// Setup a default renderer to draw the filter matches
if (filter == null)
myObjectListView.DefaultRenderer = null;
else
{
myObjectListView.DefaultRenderer = new HighlightTextRenderer(filter);
// Uncomment this line to see how the GDI+ rendering looks
myObjectListView.DefaultRenderer = new HighlightTextRenderer { Filter = filter, UseGdiTextRendering = false };
}
// Some lists have renderers already installed
HighlightTextRenderer highlightingRenderer = myObjectListView.GetColumn(0).Renderer as HighlightTextRenderer;
if (highlightingRenderer != null)
highlightingRenderer.Filter = filter;
myObjectListView.ModelFilter = filter;
}
Can someone figure out why this doesn't work?
The above code is meant to filter search results as the user types in the textbox (Like iTunes does, if you have ever used itunes). Apparently, up to this point, nothing happens. It seems like this code does not even execute.
Per this, the ObjectListView has a property named UseFiltering that is false by default and must be set to true to enable filtering.
I am having some trouble with getting the formatting to occur correctly. I believe it stems from what is probably incorrect understanding of the events that I am attempting to make the changes in.
any direction would be great
private void so_FetchData(object sender, FetchEventArgs eArgs)
{
if (m_so != null && m_so.Rows.Count > (m_soRowCount + 1))
{
DataRow soDr = m_so.Rows[m_soRowCount++];
if (soDr != null)
{
var compResID = (int) soDr["CompResID"];
var result = (ComplianceLevel) soDr["Result"];
var sectNum = (int) soDr["JobSectType"];
var sectName = soDr["S" + sectNum + "Name"] as string;
var sectTxt = soDr["S" + sectNum + "Text"] as string;
Fields["CompLev"].Value = (result == ComplianceLevel.OtherThanSerious) ? "Other Than Serious" : result.ToString();
m_sectInfo = new SectInfo(sectName, sectTxt);
m_causes = new Causes(compResID);
m_actions = new Actions(compResID);
subReport1.Report = m_sectInfo;
subReport2.Report = m_causes;
subReport3.Report = m_actions;
eArgs.EOF = false;
}
}
else
{
eArgs.EOF = true;
}
}
private void eh_BeforePrint(object sender, EventArgs e)
{
//decide where the bottom border should be draw to
if (m_actions != null && m_actions.ShouldShowBottBorder)
{
subReport3.Border.BottomStyle = BorderLineStyle.ThickSolid;
subReport2.Border.BottomStyle = BorderLineStyle.Solid;
}
else if (m_causes != null && m_causes.ShouldShowBottBorder)
{
subReport2.Border.BottomStyle = BorderLineStyle.ThickSolid;
}
else
{
subReport1.Border.BottomStyle = BorderLineStyle.ThickSolid;
}
}
the issue is that every time I step through the eh_BeforePrint method, the values always equate to false even though I step through the sub reports and the values are properly set. What is happening to cause the bool property to reset to false?
Just changing it if there are any records to print in the Fetch_Data method of each sub report.
private void Causes_FetchData(object sender, FetchEventArgs eArgs)
{
if (m_pos < m_corrs.Count)
{
if (!ShouldShowBottBorder)
ShouldShowBottBorder = true;
//...
}
}
You cannot be assured that the BeforePrint event raises exactly after the corresponding FetchData event. For example, FetchData may fire many times for several records, but due to some keep together logic in the layout engine, it may take several records before ActiveReports knows which page it will commit a section to. Therefore, it is pretty common for FetchData to be raised for several events before the corresponding BeforePrint events are raised.
If I understand your code properly there is a bigger problem though. It appears you are calculating values in your subreports (m_causes and m_actions appear to be actual subreports). If that is the case you cannot reliably calculate values in your subreports and pass them out to the parent report. Instead, you'll need to calculate those values in your parent report. However, usually you can add some shared function to calculate the value and call it from the parent report, and then pass that value into the subreports.
Comment here with some more information if you have specific questions about doing that.
On an unrelated note, you can get a pretty significant performance boost if you change the way you're initializing your subreports. Always initialize your subreports in the ReportStart event and then set their data in the format event of the section containing the Subreport control. This way you initialize each subreport once instead of initializing each subureport for every record. For example:
private void so_ReportStart()
{
subreport1.Report = new SectInfo();
subreport2.Report = new Causes();
subreport3.Report = new Actions();
}
private void Detail_Format()
{ // assuming Detail is the section containing your subreports:
((SectInfo)subreport1.Report).SetParameters(Fields["sectName"].Value, Fields["sectTxt"].Value);
((Causes)subreport2.Report).SetParameters(Fields["compResID"].Value);
((Actions)subreport3.Report).SetParameters(Fields["compResID"].Value);
}
You would setup those "Fields" values in FetchData similar to how your initializing the subreports now. Something like the following:
private void so_FetchData(object sender, FetchEventArgs eArgs)
{
if (m_so != null && m_so.Rows.Count > (m_soRowCount + 1))
{
DataRow soDr = m_so.Rows[m_soRowCount++];
if (soDr != null)
{
var compResID = (int) soDr["CompResID"];
var result = (ComplianceLevel) soDr["Result"];
var sectNum = (int) soDr["JobSectType"];
var sectName = soDr["S" + sectNum + "Name"] as string;
var sectTxt = soDr["S" + sectNum + "Text"] as string;
Fields["CompLev"].Value = (result == ComplianceLevel.OtherThanSerious) ? "Other Than Serious" : result.ToString();
/** BEGIN NEW CODE **/
Fields["sectName"].Value = sectName;
Fields["sectTxt"].Value = sectTxt;
Fields["compResID"].Value = compResId;
/** END NEW CODE **/
/** OLD CODE:
m_sectInfo = new SectInfo(sectName, sectTxt);
m_causes = new Causes(compResID);
m_actions = new Actions(compResID);
subReport1.Report = m_sectInfo;
subReport2.Report = m_causes;
subReport3.Report = m_actions;
**/
eArgs.EOF = false;
}
}
else
{
eArgs.EOF = true;
}
}
To learn more about the events in ActiveReports see the Report Events concepts topic in the ActiveReports Online Help.
To learn more about passing data into Subreports see Subreports with Run-Time Data Sources in the ActiveReports Online Help.
Scott Willeke
GrapeCity inc.