我需要保存范围编号,如果您输入范围1-10,它将保存为" 001,002,003,004,005,-010。我试过这个。
private void AutoNum()
{
if (dataGridView1.RowCount.Equals(0))
{
textBox53.Text = "001";
}
else
{
num1 = int.Parse(dataGridView1.Rows[0].Cells[0].Value.ToString());
for (row = 0; row < dataGridView1.Rows.Count; row++)
{
num2 = int.Parse(dataGridView1.Rows[row].Cells[0].Value.ToString());
if (num2 > num1)
{
num1 = num2;
}
if (row == dataGridView1.RowCount - 1)
{
memNum = num1;
textBox53.Text = memNum.ToString("000");
}
}
}
}
这是增加下一个数字。
private void Loop()
{
try
{
int txt = Convert.ToInt32(textBox35.Text);//This were I input my range number.
if (txt == 0)
{
MessageBox.Show("Invalid");
}
else
{
for (int x = 0; txt > x; )
{
memNum++;
x++;
da.InsertCommand = new SqlCommand("INSERT INTO Reg_Book VALUES (@Received_From, @Date_Reg, @Bin_Num, @Accession_Num, @Publication, @Author, @Title, @Copies, @Volume, @Remarks, @Acc_Num)", cs);
da.InsertCommand.Parameters.Add("Received_From", SqlDbType.NVarChar).Value = comboBox3.Text;
da.InsertCommand.Parameters.Add("Date_Reg", SqlDbType.Date).Value = dateTimePicker5.Value.ToShortDateString();
da.InsertCommand.Parameters.Add("Bin_Num", SqlDbType.NVarChar).Value = textBox36.Text;
da.InsertCommand.Parameters.Add("Accession_Num", SqlDbType.NVarChar).Value = textBox2.Text + "-" + textBox14.Text + "-" + textBox15.Text + "-" + textBox26.Text + "-" + memNum.ToString("000");
da.InsertCommand.Parameters.Add("Publication", SqlDbType.NVarChar).Value = textBox37.Text;
da.InsertCommand.Parameters.Add("Author", SqlDbType.NVarChar).Value = textBox47.Text;
da.InsertCommand.Parameters.Add("Title", SqlDbType.NVarChar).Value = textBox48.Text;
da.InsertCommand.Parameters.Add("Copies", SqlDbType.NVarChar).Value = textBox49.Text;
da.InsertCommand.Parameters.Add("Volume", SqlDbType.NVarChar).Value = textBox50.Text;
da.InsertCommand.Parameters.Add("Remarks", SqlDbType.NVarChar).Value = textBox51.Text;
da.InsertCommand.Parameters.Add("Acc_Num", SqlDbType.NVarChar).Value = memNum.ToString("000");
cs.Open();
da.InsertCommand.ExecuteNonQuery();
cs.Close();
}
MessageBox.Show("Saved!");
}
}
catch (Exception a)
{
MessageBox.Show(a.ToString());
}
}
这是保存它并将增加数字。是的它正在运行,如果我输入5将保存数字,它将增加001-005。我的问题是如何改进我的代码,就像用户输入范围10-15,30-50。
答案 0 :(得分:2)
如果您使用Enumerable.Range
,则可以非常快速地获取值:
//Assume the user presents this as the input
var input = "10-50";
//Split the input by hyphen
var ranges = input.Split('-');
//Get all the numbers in the range and format them as a three digit string
var allMyNumbers = Enumerable.Range(
int.Parse(ranges[0]),
int.Parse(ranges[1]))
.Select(n => n.ToString("000"))
这为您提供了一个可以播放的字符串列表:
foreach(var number in allMyNumbers)
{
Console.WriteLine(number);
}
或者如果你想要一个字符串中的所有数字:
var requestedResult = string.Join(allMyNumbers,",");
答案 1 :(得分:1)
尝试以下方法
string sRange = txtRange.Text;
int iStart = Convert.ToInt32(sRange.Split('-')[0]);
int iRange = Convert.ToInt32(sRange.Split('-')[1]);
For (int x = iStart; x < (iStart + iRange); x++)
{
//if u have entered x = 5 and range = 10
//then x will be started from 5 and ends with 14
//5,6,7,8,9,10,11,12,13,14
string sNumber = x.ToString("000");
//use sNumber to insert value
}