|
hi. how can i binding datagridview columns with entity framework that
AutoGenerateColumns=false .
<pre>var book = context.Books
.Where(b => b.ISBN == txtISBN.Text).ToList();
dataGridViewBook.DataSource = book;
|
|
|
|
|
|
Hi,
I have a program that gives me all result i search and put them in listbox.
My first problem is that it need 1-2 second when i start to search to find me a items.
Second problem is when i update items, it writes in ini file but gives me error when i try to update a listbox with edited item.
<pre>using System;
using System.Windows.Forms;
using IniParser;
using IniParser.Model;
using System.Collections.Generic;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public string myFilePath;
public string searchItem;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
searchBtn.Enabled = false;
writeIniBtn.Enabled = false;
saveBtn.Enabled = false;
AcceptButton = this.searchBtn;
}
private void openIniBtn_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.InitialDirectory = "c:\\";
openFile.Filter = "INI files (*.ini)|*.ini";
openFile.Title = "Select a INI file for edit";
if (openFile.ShowDialog() == DialogResult.OK)
{
myFilePath = openFile.FileName;
searchBtn.Enabled = true;
writeIniBtn.Enabled = true;
saveBtn.Enabled = true;
openIniBtn.Enabled = false;
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
List<string> selectedItems = new List<string>();
string item = listBox1.SelectedItem.ToString();
if (!selectedItems.Contains(item))
{
selectedItems.Add(item);
}
foreach (string i in listBox1.SelectedItems)
{
itemIdTxt.Text = i.Split('[', ']')[1];
nameInput.Text = i.Split('[', ']')[2];
}
}
private void searchBtn_Click(object sender, EventArgs e)
{
var searchItem = searchInput.Text;
var parser = new FileIniDataParser();
parser.Parser.Configuration.AllowDuplicateKeys = true;
IniData data = parser.ReadFile(myFilePath);
listBox1.Items.Clear();
searchItem = searchInput.Text;
foreach (var item in data.Sections)
{
if (item.Keys["1"].IndexOf(searchItem, 0, StringComparison.CurrentCultureIgnoreCase) != -1)
{
var foundedItems = ($"[{item.SectionName}] {item.Keys["1"]}");
ListBox list = new ListBox();
listBox1.Items.Add(foundedItems);
}
}
}
private void saveBtn_Click(object sender, EventArgs e)
{
var id = itemIdTxt.Text;
var name = nameInput.Text;
var parser = new FileIniDataParser();
parser.Parser.Configuration.AllowDuplicateKeys = true;
IniData data = parser.ReadFile(myFilePath);
data[id]["1"] = name;
parser.WriteFile(myFilePath, data);
string index = listBox1.SelectedItem.ToString();
listBox1.Items.Remove(index);
listBox1.Items.Add(name);
}
}
}
|
|
|
|
|
What is the error, and where does it occur?
|
|
|
|
|
I get error on this line
string item = listBox1.SelectedItem.ToString();
Error is :
Exception thrown: 'System.NullReferenceException' in WindowsFormsApp1.exe
System.Windows.Forms.ListBox.SelectedItem.**get** returned null.
I open a file, search for item, i get list with founded results.
When i click on item it is show i set it with this 2 lines
itemIdTxt.Text = i.Split('[', ']')[1];
nameInput.Text = i.Split('[', ']')[2];
And when i edit it in nameInput and press Save button it write in ini file and throw me an error.
So problem is in this i supose, when i try to update edited item
string index = listBox1.SelectedItem.ToString();
listBox1.Items.Remove(index);
listBox1.Items.Add(name);
|
|
|
|
|
A listbox may have no item selected. You can check that with ListBox.SelectedIndex which returns -1 if no item is selected.
In your last code snippet you are removing the selected item and adding a new one. As a result, there will be no item selected (removing the selected item will remove also the selection).
Note also that using ListBox.Items.RemoveAt passing the index is better (faster) than searching for an item by the string:
int index = listBox1.SelectedIndex;
if (index >= 0)
listBox1.Items.RemoveAt(index);
|
|
|
|
|
https://image.prntscr.com/image/SPNK0IBeSiC2dy7kG9W0kw.png
Here is a image for better view and understand of code.
As i imagine the program works is to select a item, split it into 2 sections SECTION and KEY because i need both to update a ini file where i need to use section name to write to ini file as you can see here
<pre lang="c#">
var id = itemIdTxt.Text; // read id
var name = nameInput.Text; // read name
// write to ini file
var parser = new FileIniDataParser();
parser.Parser.Configuration.AllowDuplicateKeys = true;
IniData data = parser.ReadFile(myFilePath);
data[id]["1"] = name;
parser.WriteFile(myFilePath, data);
</pre>
|
|
|
|
|
But that is not related to my post where I tried to explain while your code fails when trying to access selected item(s) when nothing is selected.
Depending on what you want to do (replace), you might search for the old item, update the selection programmatically to ensure that it is always set (to what should be set), or track the index of the item to be replaced to be used in the button handler.
|
|
|
|
|
that seems to be so complicated for beginner.
|
|
|
|
|
Hi, i have a problem with setting a file path to a public variable.
So, what my code must do :
- open file
- set full path to a file so i can read it in another function
- in other function open ini file for reading
but i cant get a file path to it
Here is a code ;
using System;
using System.Windows.Forms;
using IniParser;
using IniParser.Model;
using System.Collections.Generic;
using System.IO;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public string myFilePath;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
searchBtn.Enabled = false;
writeIniBtn.Enabled = false;
saveBtn.Enabled = false;
}
private void openIniBtn_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.InitialDirectory = "c:\\";
openFile.Filter = "INI files (*.ini)|*.ini";
openFile.Title = "Select a INI file for edit";
if (openFile.ShowDialog() == DialogResult.OK)
{
string myFilePath = openFile.FileName;
searchBtn.Enabled = true;
writeIniBtn.Enabled = true;
saveBtn.Enabled = true;
openIniBtn.Enabled = false;
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count != 0)
{
ListViewItem item = listView1.SelectedItems[0];
string output = item.Text.Split('[', ']')[1];
string outputt = item.Text.Split('[', ']')[2];
itemIdTxt.Text = output.ToString();
nameInput.Text = outputt.ToString();
}
}
private void searchBtn_Click(object sender, EventArgs e)
{
MessageBox.Show("my file path : " + myFilePath);
|
|
|
|
|
That's because you've hidden the field with a local variable:
public string myFilePath;
private void openIniBtn_Click(object sender, EventArgs e)
{
...
string myFilePath = openFile.FileName;
...
}
Remove the local variable, and use the field instead:
private void openIniBtn_Click(object sender, EventArgs e)
{
...
myFilePath = openFile.FileName;
...
}
If you want to be absolutely safe, you can even prefix the field name with this. to ensure that it uses the field and not a local variable:
private void openIniBtn_Click(object sender, EventArgs e)
{
...
this.myFilePath = openFile.FileName;
...
}
Scopes | Basic concepts | C# Language Reference | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks man this solved my problem.
|
|
|
|
|
Is there a known API to Google Images[^] so that it is possible to use it automatically with local files?
|
|
|
|
|
Try the Google developers website.
|
|
|
|
|
I hope someone can help me. I develop my first C# WPF Program and I need an input control for IP Address. To my big surprise there isn't a control for this which I think is pretty strange. In C++ I have something like that.
Okay so far I have read I could use a masked textbox from the extended wpf toolkit. But I have 2 problems with that:
First of all when I use a mask like 000.000.000.000 and someone wants to enter an address like 192.168.1.1 I would get 192.168.001.001 But I don't want to see leading zeros
the second problem is that someone could enter an address like 500.300.400.800. How can I handle this?
|
|
|
|
|
|
Thank you for the link but that's what I did the entire day yesterday but I found no page that answered my 2 questions. Either the links weren't available anymore or they just said that you should use masked textboxes. But non of them has explained how to handle my two problems with the masked text boxes
I also found a dll that offered an IP Address control but I couldn't change the font size for example for the control at all so I can't use it either
|
|
|
|
|
Try posting your question in the WPF forum, you may get a better answer.
|
|
|
|
|
Don't you see that an IPv4 address consists of 4 bytes?
Now write a value converter between string and IpAddress which handles that formatting.
That can be based on the following snippet showing both conversions:
IPAddress address = IPAddress.Parse("192.168.1.1");
byte[] bytes = address.GetAddressBytes();
string formatted = $"{bytes[0]:D3}.{bytes[1]:D3}.{bytes[2]:D3}.{bytes[3]:D3}";
|
|
|
|
|
Why not just use a normal TextBox control, combined with IPAddress.TryParse[^] to parse the input?
The problem with the old IP address control is that it's limited to IPv4 addresses. If your application ever needs to deal with IPv6 addresses, that control won't work.
IPv6 - Wikipedia[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Member 13196574 wrote: To my big surprise there isn't a control for this which I think is pretty strange
Really? That's a not-so-commonly used control so I'm not surprised it doesn't exist.
On the other hand, up until about seven years ago, WPF didn't have a data grid. Now THAT was a surprise!
|
|
|
|
|
I have created SQL store procedure.
CREATE PROCEDURE spGetMiti (
@engdate varchar(10)
) with encryption AS
BEGIN
DECLARE @RC int
DECLARE @RomanDate DATETIME
DECLARE @PrnLine nvarchar (4000)
EXEC @RC = [HRDB].[dbo].[s_GetNepaliDate] @engdate, @RomanDate OUTPUT
SELECT @PrnLine = isnull(CONVERT(nvarchar, @RomanDate, 111), '')
SELECT @PrnLine [Tarikh]
END
it displays in correct result after executing store procedure
EXEC spGetMiti '1978/02/03'
Tarikh > 2035/10/21
IN c#, class MtCommom
private string _engdate = string.Empty;
public string Engdate
{
get { return _engdate; }
set {
if (value.Length < 10)
{
throw new Exception("Correct date is required");
} _engdate = value;
}
}
public DataTable GetMiti()
{
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@engdate", _engdate)
};
return DAO.GetTable("spGetMiti", null);
}
in Button Press
MtCommon mtcom = new MtCommon();
mtxtDOB.Text = string.Empty;
mtcom.Engdate = mtxtEngDate.Text;
DataTable dt = mtcom.GetMiti();
mtxtDOB.Text = dt.Rows[0]["Tarikh"].ToString();
Error Displays
"Procedure or function 'spGetMiti' expects parameter '@engdate', which was not supplied."
|
|
|
|
|
public DataTable GetMiti()
{
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@engdate", _engdate)
};
return DAO.GetTable("spGetMiti", null);
}
In the above code you create an array of SqlParameter values, and add one labelled @engdate . However, when you call the stored procedure, I do not see that the parameter array is being passed to it.
|
|
|
|
|
CREATE PROCEDURE spGetMiti (
@engdate varchar(10)
) with encryption AS
BEGIN
DECLARE @RC int
DECLARE @RomanDate DATETIME
DECLARE @PrnLine nvarchar (4000)
EXEC @RC = [HRDB].[dbo].[s_GetNepaliDate] @engdate, @RomanDate OUTPUT
SELECT @PrnLine = isnull(CONVERT(nvarchar, @RomanDate, 111), '')
SELECT @PrnLine [Tarikh]
END
I think, there is a parameter "@engdate". Isn't it parameter?
|
|
|
|
|
Yes, but you never send the parameter when you call the SP.
|
|
|
|